tags:

views:

55

answers:

1

When I enter 42 for the seed value, it provides a certain sequence of numbers and reproduces the same ones every time the program is used. But the sequence of numbers in my program doesn't match up with the sequence of numbers in another program. I need to figure out how to make both of them provide the same output of random numbers when both seed values are set to 42. Is this the correct format for getting a seed value from the user? What could be causing the numbers to not match up? Thanks for the help.

public class SampleTest {

 public static void main(String[] args) {

        System.out.println("Please enter a seed value: ");

     Scanner in = new Scanner(System.in);
     seed = in.nextInt();
     Random ranGen = new Random(seed);
+1  A: 

You say that you are seeding the random number generator with the same seed, making the same calls, and yet the number sequences are different.

I think there are two plausible explanations for what is happening:

  1. Your two applications are in fact not seeding the generator and using it the same way. There are lots of mistakes that could have been made, including accidentally using more than one generator (as per @PD's comment).

  2. The applications are running on platforms with different implementations of the generator class. (This would be contraindicated by the javadocs for Random, which specify the exact generator formulae that should be used.)

The way to figure out which of these two possibilities is occurring is to write a simple test application that just prints the sequence of numbers produced by a Random instance with a given seed, then run it on both machines.

Stephen C