# Random Number problem

• November 26th, 2012, 11:16 AM
Optimizer
Random Number problem
This is my code:

Code :

``` public background() { super(8, 8, 60); setBackground("cell.jpg"); maakland(); maakdijk(); maakWater(); } public void maakland() { land[] land = new land[16]; int b = 0;   for (int y = 6; y < 8;){   for (int x = 0; x < 8;){   land[b] = new land(); addObject(land[b], x, y); ++x; ++b; } ++y; } } public void maakdijk() { dijk[] dijk = new dijk[24]; int b = 0;   for (int y = 3; y < 6;){ for (int x = 0; x < 8;){   dijk[b] = new dijk(); addObject(dijk[b], x, y); ++x; ++b; } ++y; } } public void maakWater() { Water[] Water = new Water[32]; int b = 0;   for (int y = 0; y < 3;){ for (int x = 0; x < 8;){   Water[b] = new Water(); addObject(Water[b], x, y); ++x; ++b; } ++y; } } public void randomWater(int howMany) { for(int i=0; i<howMany; i++) { Water water = new Water(); int x = Greenfoot.getRandomNumber(8); int y = Greenfoot.getRandomNumber(8); } } }```

As my backgrond i have the first 3 lines: Water second 3 lines: Dike (that needs to turn to water after playing the animation). third 2 lines: Land.

As you can see i made the background but like i said i want the dike (dijk) to turn into water using randomnumber.. or getoneobjectatoffset..
Any idea how i can do this?
• November 26th, 2012, 01:23 PM
Norm
Re: Random Number problem
Quote:

i want the dike (dijk) to turn into water using randomnumber
If there are n lines that are dike, you want to randomly select one of those lines and change it to water.
Assign the lines a number from 0 to n-1 and use the Random number generator to randomly generate a number in that range. See the API doc of the Random class for methods that will do that.