mardi 19 avril 2016

Java random seed

I have a java assignment I'd like some help with. There is a tester program that uses a random number seed and it's giving me problems coming up with the same result.

 boolean filled = true;
 while(filled)
 {
     int j = random.nextInt(GRID_SIZE);
     int k = random.nextInt(GRID_SIZE);
     if(grid[j][k] < 2)
     {
         int newTile = random.nextInt(100);
         if(newTile < 90) newTile = 2;
         if(newTile >= 90) newTile = 4;
         grid[j][k] = newTile;
         filled = false;
     }
 }

The random object is passed in as a parameter to the class and has some set seed in the test file. When I run this method twice I get the following matrix. GRID_SIZE is a global variable.

[0 0 0 0]
[0 0 0 0]
[0 2 2 0]
[0 0 0 0]

But I should be getting

[0 0 0 0]
[0 0 0 0]
[0 2 0 0]
[0 0 2 0]

Any idea what's wrong with my code?




Aucun commentaire:

Enregistrer un commentaire