Random number generators By Bartek Wohlert and Arrian
Random number generators By Bartek Wohlert and Arrian Torres
§Java uses what is called a Linear Congruential Method to generate a random number §Java includes the Random class in the java. util package for generating random numbers.
§A random number generator is simply enough a tool that allows the person who's making the code to generate a random number. §Because the sequence eventually repeats, random number in a computer application are really pseudorandom.
§It can either return a random integer or a random double integer §They can have a specified range. i. e: 1 -10
§ A random number generator has many different and valuable uses to it § For most reasons, using a random number generator can help make a guessing game created in java function much more better § For more grander reasons, random number generators are used in lotteries with very difficult algorithms to break.
§ To generate a random number in a range this code is used to do that (high – low + 1) * Math. random() + low § Note that high represents the largest number & low the lowest number in the range. To randomly generate a number between 1 -10: (10 – 1 + 1) * Math. random() + 1
An example of a random number generator
§ There are different variants to the random number generator. § A basic change that can be done is determining the range that your random number generator § You can also generate a number in double or int form § Double: 0. 0 – 1. 0 § Int: 1 -10
- Slides: 9