Click to See Complete Forum and Search --> : Filling an array with random numbers in Java


deathadder
12-29-2004, 07:30 PM
I've been trying to fill an array of 10 ints with 'random' numbers between 0 and 10. How ever my code generates one number and then keeps using that. So when I display the array I get something like:


Num 1 = 3
Num 2 = 3
Num 3 = 3
.....
Num 10 = 3



I've been searching around the web, for hours now and finding references to 'seeds' but I haven't been taught about them yet and I'm getting confused. Could someone explain or point me to a google that explains this in depth. I've imported java.util.Random. The code I'm using is below.

Thanks for any help!


int x [] = new int [10];

for(int y = 0 ; y < x.length ; y++)
{
x[y] = randomNum();
}

....

public static int randomNum()
{
Random generator = new Random();
return generator.nextInt(10);
}

Arjay
12-29-2004, 08:36 PM
Take a look in the API on the java.sun website..

http://java.sun.com/j2se/1.4.2/docs/api/

Random is under java.lang.Math

java.lang.Math.random()

Hope that helps

Arjay
12-29-2004, 09:26 PM
Here's my attempt...



class numbers {

public static void main(String[] args) {

int maxSize = 10;
int[] randomArray = new int[maxSize];

for (int i = 0; i < maxSize; i++) {
int n = (int) (Math.random()*(maxSize-1));
randomArray[i] = n;
System.out.println(n);
}
}
}

Calipso
12-29-2004, 09:44 PM
I thought id point something out cause it might cause you some problems.
0-10 is 11 numbers.
Common way of making a mistake.

check out this site, it helped me with random numbers.

http://www.cs.geneseo.edu/~baldwin/reference/random.html

[edit] i just thought of something, i think your code only generates one random number. I think that if you put the random number generator in a loop, it would create a new random number every time. Seems to me that your method runs once, returns the value and thats it. Thats why when ever you call the variable it always is the samen number. In your example it was a 3. Just my 2 cents.

Arjay
12-29-2004, 10:33 PM
I'm not sure what you mean, sorry, i only ran it a few times to see if i got different output each time. Here is some output:

1st run: 4 5 0 5 3 7 1 5 8 2
2nd run: 8 1 6 2 0 7 3 5 0 2
3rd run: 7 6 2 2 5 3 2 7 5 1

It seemed to work fine, i think.

Cheers

deathadder
12-30-2004, 08:58 AM
Arjay I've used your code and it works great, thanks for the suggestions. Also was 'I'm not sure what you mean' for me or Calipso?

Calipso thanks for the link, and suggestions.

Thanks again to both of you!

Arjay
12-30-2004, 09:55 AM
It was Calipso's suggestion i wasn't sure about deathadder. Maybe the the first random number is being inserting into the array. Then the next one is created and overwrites the first one, however each one is displayed during each loop. Ideally the 'i' should be incremented and the next random number takes that next slot.

I think that is taken care of with the for loop. I'm on windows right now, perhaps a way to check though is to display each element individually to make sure all the elements have a number. I just noticed that i was sending the contents of 'n' to standard output rather than what was in the array.

Alternatively maybe someone else can shed some light on the matter.

Cheers

bwkaz
12-30-2004, 10:35 AM
The problem is that when you create a new java.util.Random object, you need to seed it with a different value. The Random class does not create truly random numbers -- it generates pseudo-random numbers based on a seed value. If the seed value is set to the same thing every time the nextInt() function is called then you'll always get the same "random" number.

In your case, that number is 3.

The java.sun.com API reference for java.util.Random.next() (the function that nextInt calls) says that the Random.next() function is implemented with this code:

synchronized protected int next(int bits) {
seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
return (int)(seed >>> (48 - bits));
} Notice how it's totally deterministic based on the value of "seed".

So, for a solution, you could make the Random object static inside your randomNum() function. Then it wouldn't be recreated every time you called that function, and the seed wouldn't be reset.

Alternately, every time you create a Random object, you could seed it with the current number of seconds past the Epoch (in C, this is done with an srand(time()); call; I'm not sure how to do it in Java).

Arjay
12-30-2004, 04:15 PM
Cheers bwkaz. The only examples i have ever seen have used the Math.random method, well in the two Java books i have anyway. There was a reference to the method you have shown above at the back of one of the books, i'll look into it further when i have a moment.

Thanks for pointing that out

Cheers :)

Calipso
01-01-2005, 02:34 PM
Im assuming that when you said you didnt know what i meant you were referring to me saying that it creates one random number and keeps using it.

When I wrote this I was referring to the original code in the first post.....And it was just a guess. To me it seemed that it only created a random number once and then kept using the same value over and over. I didnt actually run the code to see how it runs because I was booted in windows at the time and didnt have a Java IDE installed so couldnt actually try running the code.

Im no Java expert, far from it, I only took 2 basic java classes and play around with it on occasion. Just thought Id take a shot and help to best of my ability :D

Cheers....happy new year.