0

Possible Duplicate:
Java random always returns the same number when I set the seed?
Java Random Numbers Using a Seed

Hi, This is my code. I am trying to generate 2 random numbers simultaneously using a seed i.e. 15416640. The numbers that are getting generate are not really random.

Random radiusGenerator = new Random(15416640);

Random angleGenerator = new Random(15416640);

try
{
    for(int i=1; i<=sequenceNumber; i++)
    {
        double radius =  (0.5 - (0.5 * Math.sqrt(1-radiusGenerator.nextDouble())));
        double angle = angleGenerator.nextDouble();
        angle = angle*(Math.PI*2);

        System.out.print(radius+"      "+ angle +"\n");
     }

Please Help...Thanks!

Community
  • 1
  • 1
Rahul Bhatia
  • 1,005
  • 2
  • 13
  • 18

2 Answers2

1

That's totally normal and a feature : in a Pseudo Random Generator, the seed defines the sequence of numbers that will be generated.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

Use one Random object, and generate everything you want. Since you initialize 2 Random object with the same seed, they will generate the same number if you call with the same method.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162