Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Random Class and its Methods

Similar presentations


Presentation on theme: "The Random Class and its Methods"— Presentation transcript:

1 The Random Class and its Methods
Random Numbers Using The Random Class November 12, 2018 The Random Class and its Methods

2 Random Numbers in Applications
Some applications such as games and simulations require the use of randomly generated numbers. Tossing a coin: an even number may represent a head and an odd number may represent a tail A random number between one and six may represent the roll of one die A random number between 1 and 52 may represent a particular card in a deck of cards A roulette wheel has 38 slots numbered 0-36 and 00. One may use a random number between 0 and 37 to simulate a spin of the wheel. November 12, 2018 The Random Class and its Methods

3 The Random Class and its Methods
The Java API has a class named Random for this purpose. To use the Random class, use the following import statement and create an instance of the class. import java.util.Random; Random randomNumber = new Random(); November 12, 2018 The Random Class and its Methods

4 Some Methods of the Random Class
Description nextDouble() Returns the next random number as a double. The number will be within the range of 0.0 and 1.0. nextFloat() Returns the next random number as a float. The number will be within the range of 0.0 and 1.0. nextInt() Returns the next random number as an int. The number will be within the range of an int, which is –2,147,483,648 to +2,147,483,648. nextInt(int n) This method accepts an integer argument, n. It returns a random number as an int. The number will be within the range of 0 to n.

5 The Random Class and its Methods
Using the Random Class The methods in the previous table generate uniformly distributed random numbers Each value in the range is equally likely to occur The type of data returned is determined by the name of the method (see previous slide – nextDouble, nextInt, …) November 12, 2018 The Random Class and its Methods

6 Range of Random Numbers
The nextInt (n) method generates a random number in the range between 0 and n. 0 is included in the range but n is not included in the range nextInt (4) would generate 0, 1, 2, or 3 with equal probability. No other result is possible. To generate a random integer between min and max inclusively, use an expression such as the following where r is an object of Random: int num = r.nextInt (max – min + 1) + min; // random int between 11 and 15 inclusively int ans = r.nextInt (5) ; November 12, 2018 The Random Class and its Methods

7 The Random Class and its Methods
Range of Doubles To generate a double between d1 and d2 use: double rand = r.nextDouble ( ) * (d2 – d1) + d1; To generate a random double between 800 and 1000 we would use double value = r.nextDouble( ) * (200.) ; November 12, 2018 The Random Class and its Methods

8 Random int’s between 0 and 99
Sample output Random int’s between 0 and 99 November 12, 2018 The Random Class and its Methods


Download ppt "The Random Class and its Methods"

Similar presentations


Ads by Google