By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

My random numbers are not random :(

P: n/a
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 java version I have installed is
j2sdk-1_4_2_03-windows-i586-p taken from the distribution name.
Any ideas how to actually make it random.

Cheers
Andy
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Random randomNumberGenerator = new Random();
for( int i=0; i<10; i++)
{
int num = randomNumberGenerator.nextInt(10);
System.out.println( num );
}

"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 java version I have installed is
j2sdk-1_4_2_03-windows-i586-p taken from the distribution name.
Any ideas how to actually make it random.

Cheers
Andy

Jul 17 '05 #2

P: n/a
Consider having new outside the loop

Random randomNumberGenerator;
randomNumberGenerator = new Random();

for( int i=0; i<10; i++) {
int num = randomNumberGenerator.nextInt(10);
System.out.println( num );
}

"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 java version I have installed is
j2sdk-1_4_2_03-windows-i586-p taken from the distribution name.
Any ideas how to actually make it random.

Cheers
Andy

Jul 17 '05 #3

P: n/a
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 );
}
Jul 17 '05 #4

P: n/a
ok cheers folks. It dawned on me about 2am this morning. :)

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.