AndyW <foo_@bar_no_email.com> wrote in message news:<32********************************@4ax.com>. ..
hey folks,
Just have some code as follows:-
for( int i=0; i<10; i++) {
Random randomNumberGenerator;
randomNumberGenerator = new Random();
int num = randomNumberGenerator.nextInt(10);
System.out.println( num );
}
The problem is I am getting results such as 33333555555999999
The problem is that you're declaring a new random number generator
each time through the loop. The default constructor of the Random
class initializes the random sequence with a seed based on the system
time. Thus, all Random objects created within the same millisecond
will have the same sequence. And in your code you're throwing away
each Random object after only retrieving the first value from it. This
should work better:
Random randomNumberGenerator;
randomNumberGenerator = new Random();
for( int i=0; i<10; i++) {
int num = randomNumberGenerator.nextInt(10);
System.out.println( num );
}