Hi I wonder if somebody can help me with this. This programs simulates throwing a die 20 times:

import java.util.Random; // program uses class Random

public class RandomIntegers 
{
   public static void main( String[] args )
   {      
      Random randomNumbers = new Random(); // random number generator
      int face; // stores each random integer generated

      // loop 20 times
      for ( int counter = 1; counter <= 20; counter++ ) 
      {
         // pick random integer from 1 to 6
         face = 1 + randomNumbers.nextInt( 6 );

         System.out.printf( "%d  ", face ); // display generated value

         // if counter is divisible by 5, start a new line of output
         if ( counter % 5 == 0 )
            System.out.println();
      } // end for
   } // end main
} // end class RandomIntegers

I have read that often - maybe not in this instance but in general - for debugging purposes it helps to be able to generate always the same sequence of number, and to achieve that rather than having Random randomNumbers = new Random(); we should provide a seed argument like this Random randomNumbers = new Random(seedValue);
So if I change that particular line in the above program to something like Random randomNumbers = new Random(15); the sequence of numbers generated is always the same at each roll of the die. Now, my question now is this: which number should I use as seed? I mean can I use any number, say 1, 30, 34, 100? What is the difference between one number or the other one? SO what's the difference if I do Random randomNumbers = new Random(15); or if I do Random randomNumbers = new Random(25); I mean the seed is where the random generator is supposed to start generating numbers from?Uhm...maybe I need some clarifications on that...
thanks

Just like a random number, the seed of a random number generator is supposed to be meaningless. Since we have pseudorandom numbers instead of actual random numbers it might be possible to work out some meaning in the seed you give to Random, but unless you find that the pseudorandom numbers aren't random enough for your purposes then there is no reason to worry about that. Just pretend that the seed is a totally meaningless number and it doesn't matter what seed you choose.

If you want to test your application with various sequences of random numbers you could try with a seed of 0, then try again with 1, then 2, and so on until you are satisfied your application always works. Or you could choose the seed by rolling some real dice. If Random is doing its job well, there's nothing anyone can tell you about what numbers will be generated by a seed except by trying the seed.

I mean the seed is where the random generator is supposed to start generating numbers from?

The class uses a 48-bit seed, which is modified using a linear congruential formula.

(Source: Java API - java.util.Random)

This is a linear congruential pseudorandom number generator,
as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 3: Seminumerical Algorithms, section 3.2.1.

(Source: Java API - java.util.Random.next(int))

For an introductory read on Linear Congruential Generators, take a look at Julienne Walker's Eternally Confuzzled page about Random Numbers.

which number should I use as seed?

For general use the easiest thing you can do is calling the no argument constructor of Random.
It will seed the random number generator automagically to a value that is very likely not going to be the seed of any other invocation of that constructor.

Here's how it is stated in the Java API:

Creates a new random number generator.
This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.

(Source: Java API - java.util.Random: Random())

Edited 3 Years Ago by mvmalderen

thanks for the replies guys. When I asked the question, I suspected I didn't really need to worry about the number that goes as seed, it was just something I was curious to know. After having read your answers and duly checked the links provided, I came to realize that sometimes ignorance is bliss : - )!
thanks

This article has been dead for over six months. Start a new discussion instead.