Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Random Class Random Number Methods

 

General

Methods Summary

Example 1

Example 2

Example 3 (Scaling)

Scaling - The Code

 

 

General

 

Constructor: Random ( )

 

Creates a new random number generator based on the current time to nearest millisecond

 

The numbers are both negative and positive

 

Code to calculate largest and smallest integers and doubles

 

Methods Summary

 

Method Summary (Partial)

double

nextDouble()
   Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's

    sequence.

float

nextFloat()
   Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

int

nextInt()
   Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

int

nextInt(int n)
   Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this

    random number generator's sequence.


Example 1 Using Random Class

 

import java.util.*;

public class MathRandom

{

   

public static void main (String[ ] args) 

    {

         Random randomizer = new Random();

         int number1 = randomizer.nextInt();

         double number2 = randomizer.nextDouble();

         int number3 = randomizer.nextInt(10);         

         System.out.println("OUTPUT: nextInt(), nextDouble(), nextInt(10)");

         System.out.println(number1);

         System.out.println(number2);

         System.out.println(number3);

    }

}

 

Output

 

OUTPUT: nextInt(), nextDouble(), nextInt(10)

215063897

0.13314404262070378

7

 

Example 2 Using Random Class

 

import java.util.*;

public class RandomIntegers

{

  public static void main (String[ ] args)

  {

    Random randomizer = new Random();

    for(int i = 1; i<9;i++)

    {

            int number1 = randomizer.nextInt();

            System.out.print(number1 + "   ");

            if (i%4==0)

            {

                     System.out.println();

                     System.out.println();

            }

     }

   }

}

   

 

OUTPUT

 

-1710261529 -2035096370 31083296 2077075681


-1474238010 -2002663708 448426528 -1684975072

Example 3: Scaling

import java.util.*;

public class MathRandom

{

    public static void main (String[ ] args)

    {

      int high = 30;   //high number in the range of numbers

      int low = 10;    //low number in the range of numbers

        Random randomizer = new Random();

       

        for (int i = 1; i < 150; i++)

        {

          int number = randomizer.nextInt(high-low + 1) + low;        

          System.out.print(number + " ");

          if (i % 10 == 0)

          {

            System.out.println();

          }

        }

 

    }

}

 

Output

 

17 30 26 28 18 25 22 11 13 15

30 28 26 27 13 15 12 29 19 13

11 11 15 14 27 19 13 20 19 21

13 12 12 16 25 23 11 22 25 19

12 16 27 16 29 21 23 11 21 23

14 19 23 18 27 26 22 30 30 17

10 25 11 17 16 24 23 21 15 22

21 20 18 28 30 18 23 18 23 18

29 21 22 29 12 28 21 24 12 15

20 19 24 25 20 24 27 18 12 18

10 28 29 29 25 26 16 24 21 18

25 22 24 27 23 23 23 12 11 30

25 23 26 19 21 29 30 23 20 11

13 13 21 18 23 25 27 29 27 17

30 20 27 26 23 28 25 14 18

 

Scaling - The Code

int number = randomizer.nextInt(high-low + 1) + low;    

  Subtracting Low from High gives the high point of the range - low. 1 must be added since the generator returns a number 1 less than the

      number entered.

  The range after the above step goes from 0 to the number desired minus low (since low was subtracted from it).

  Adding low back in forces the numbers to be in the range low to high.