Jump to content

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 = GRID_SIZE*GRID_SIZE;
             int k = random.nextInt(j);
             int l = k % GRID_SIZE;
             k = k/GRID_SIZE;
             while(grid[k][l] < 2)
             {
                     int newTile = random.nextInt(100);
                     if(newTile < 90) newTile = 2;
                     if(newTile >= 90) newTile = 4;
                     grid[k][l] = 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]
[2 2 0 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?

EDIT: Updated the code and result. I was supposed to be using one number from 0-15 not two from 0-3. I'm still not getting the right result.

There is also global variable and random parameter passed in like this.

private final Random random;

public Board(int size, Random random) {
this.random = random;
...etc.

I don't know if that is the correct way to do it.

Link to comment
https://linustechtips.com/topic/584578-help-with-some-java-programming/
Share on other sites

Link to post
Share on other sites

1 minute ago, Fireknives said:

I just took a quick look but do you see any reason why that out of place 2 might not be able to get into that spot? (I am trying to help you figure it out yourself)

Sorry I really don't see it. I thought it was the if at first but that's only the value not position.

Link to post
Share on other sites

So everything else should be okay, if(grid[j][k] < 2) and beyond, but somehow when you are using NextInt it is not progressing it to the NextInt. Because J and K are getting the same value. Well actually you might be reseeding the nextInt since you are sending in Grid_Size again. Been a year or two since I did java though.

 

Edit: Yeah just try it with  int k = random.nextInt(); and see what happens. Doesn't hurt to try. 

Link to post
Share on other sites

32 minutes ago, Fireknives said:

So everything else should be okay, if(grid[j][k] < 2) and beyond, but somehow when you are using NextInt it is not progressing it to the NextInt. Because J and K are getting the same value. Well actually you might be reseeding the nextInt since you are sending in Grid_Size again. Been a year or two since I did java though.

Ok so I'm supposed to only use one random number that goes from 0 to 15 instead of two from 0 to 3.

But I still have the same problem. I'm supposed to get 9 and 14 but I get 8 and 9.

EDIT: just updated post.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×