# Random Number Arrays

Printable View

• May 28th, 2013, 09:42 AM
Wazza.roberts
Random Number Arrays
Ok so im trying to create two arrays of different random numbers, the problem is both arrays have exaclty the same numbers, now i have tried using one Random and the setting different seeds before generating the numbers but this does not help.

Heres the code

Code :

```Random rnd = new Random();   //Create tempchannels for scene1 for(int i=0;i<=511;i++){ tempChannels[i] = rnd.nextInt(255); } scene1 = new Scene(tempChannels);   //Create tempchannels for scene1 for(int i=0;i<=511;i++){ tempChannels[i] = rnd.nextInt(255); } scene2 = new Scene(tempChannels);   compScene1 = scene1.getChannels(); compScene2 = scene2.getChannels();```
• May 28th, 2013, 09:48 AM
jbarke12
Re: Random Number Arrays
I bet if you defined two different arrays to pass into your scenes you wouldn't have this problem
• May 28th, 2013, 09:56 AM
Wazza.roberts
Re: Random Number Arrays
Thats great it worked, but now to be awkward i want to understand why my original logic didnt work. If im setting all 512 parts of the array to a random number then assigning that to scene1, then i overwriting the array and assigning that to scene2 why did the numbers come out the same? The random number seed is set at the start so once it has generate the first 512 numbers the second 512 wuld essentially be continuing from the pointnit left off. But it seems to be restarting from the same seed point, if that makes sense.
• May 28th, 2013, 10:33 AM
angstrem
Re: Random Number Arrays
Quote:

im setting all 512 parts of the array to a random number then assigning that to scene1, then i overwriting the array and assigning that to scene2
How much arrays do you manipulate during this operation?
• May 28th, 2013, 10:51 AM
Wazza.roberts
Re: Random Number Arrays
Three arrays mainly. One to hold the random numbers generated that is then passed into a new object of scene, ie scene1. This same array is then has its values overwritten and passed to a new object of scene ie scene2 I then use two more arrays to read the arrays from the each object ie scene1 and scene2
• May 28th, 2013, 11:52 AM
angstrem
Re: Random Number Arrays
Do you remember, that you pass to the methods not the arrays themselves, but merely references to the array location in JVM heap?
• May 28th, 2013, 12:40 PM
Wazza.roberts
Re: Random Number Arrays
Of course , how silly of me to make such a mistake, cheers for the help