473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

My random numbers are not random :(

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
4 5312
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
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
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
ok cheers folks. It dawned on me about 2am this morning. :)

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
10
by: Marshall Belew | last post by:
I'm trying to synchronize a network app that uses random numbers generated by System.Random. Rather than pass every randomly generated number, I just pass the seed. I'm seeing a result that leads...
5
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
11
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not...
8
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
17
by: Xoomer | last post by:
Hi, Can some post a code with simple Unique Random Numbers. Random numbers stored in arr but unique.
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.