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

True Random Numbers?

P: n/a
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 using to get random numbers was

Random rn = new Random(System.currentTimeMillis());

but it seems that the system doesn't update the milliseconds often enough to
cause a true randomaziation (i ran just the System.currentTimeMillis() in a
for loop 200 times and only got 3 unique results)

any suggestions on how to get a quantity of real random numbers?

Thanks
Nick


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
I had a similar problem and added this in my loop:

Thread.sleep(1,2); //sleep fo 1 millis, 2 nanos to generate a more
random seed

It will slow the number generation but will guarantee uniquely random
numbers.

"Nicholas Geraldi" <ni*********@taconic.net> wrote in message
news:3f**********@corp.newsgroups.com...
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 using to get random numbers was

Random rn = new Random(System.currentTimeMillis());

but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a for loop 200 times and only got 3 unique results)

any suggestions on how to get a quantity of real random numbers?

Thanks
Nick


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

Jul 17 '05 #2

P: n/a
The world rejoiced as "Nicholas Geraldi" <ni*********@taconic.net> wrote:
any suggestions on how to get a quantity of real random numbers?


Most operating systems these days have a device called "/dev/random",
which collects values from hardware devices and combines it
cryptographically to generate highly random values.

Since that is a limited resource, there is often also a "/dev/urandom"
device which uses crypto functions to generate very good pseudorandom
data at faster rates...
--
If this was helpful, <http://svcs.affero.net/rm.php?r=cbbrowne> rate me
http://cbbrowne.com/info/linuxxian.html
MICROS~1: Where do you want to go today? Linux: Been there, done
that.
Jul 17 '05 #3

P: n/a
You should take a look at the SecureRandom class that comes with Java.
Of course, you don't want to re-seed or re-create it in the middle of
the loop.

Nicholas Geraldi wrote:
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 using to get random numbers was

Random rn = new Random(System.currentTimeMillis());

but it seems that the system doesn't update the milliseconds often enough to
cause a true randomaziation (i ran just the System.currentTimeMillis() in a
for loop 200 times and only got 3 unique results)

any suggestions on how to get a quantity of real random numbers?

Thanks
Nick


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


Jul 17 '05 #4

P: n/a
could i get some documentation or an example of this?

thanks,
Nick


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #5

P: n/a
Christopher Browne wrote:
The world rejoiced as "Nicholas Geraldi" <ni*********@taconic.net> wrote:
any suggestions on how to get a quantity of real random numbers?


Most operating systems these days have a device called "/dev/random",


Most *Unix*-like operating systems, that is. So, using /dev/random would
not be platform independant, and probably not wanted anyway, since Nicholas
seems to be a Windows user.

That said, if you don't care about platform independance, /dev/(u)random is
pretty nice.

--
Min virkelige mailadresse er rot13(un****@avyfra.pbz).

Jul 17 '05 #6

P: n/a
Ty
Random Numbers
int high=9;
int low=4;
range=high-low+1;

int randomNumber = (int)Math.random(range+low);

or for doubles

double high=6.98;
double low=5.43;
double range=high-low;
double randomNumber = Math.random(range+low);

Nicholas Geraldi <ni*********@taconic.net> wrote in message
news:3f**********@corp.newsgroups.com...
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 using to get random numbers was

Random rn = new Random(System.currentTimeMillis());

but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a for loop 200 times and only got 3 unique results)

any suggestions on how to get a quantity of real random numbers?

Thanks
Nick


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

Jul 17 '05 #7

P: n/a
"Nicholas" == Nicholas Geraldi <ni*********@taconic.net> writes:

Nicholas> Im looking for a decent random number generator. Im
Nicholas> looking to make a large number of random numbers (100 or
Nicholas> so, if not more) in a short period of time (as fast as
Nicholas> possible).

What is wrong with just using a single java.util.Random object?

Nicholas> the function i was using to get random numbers was

Nicholas> Random rn = new Random(System.currentTimeMillis());

Nicholas> but it seems that the system doesn't update the
Nicholas> milliseconds often enough to cause a true randomaziation
Nicholas> (i ran just the System.currentTimeMillis() in a for loop
Nicholas> 200 times and only got 3 unique results)

What you are describing is probably not a random number
generator. What you might want to do is generate a single Random
instance at start up, seed it with the current time (which is not a
good practice for cryptography type applications, but you did not tell
us what you wanted to do with the numbers), and call nextInt() each
time you want a new random number.

Nicholas> any suggestions on how to get a quantity of real random
Nicholas> numbers?

Don't change your Random instance. Create one, seed it once, use it
for as long as you can (see java.lang.math.Random() for example). Your
problem is reduced to making sure you get a new seed each time your
program runs. If you are not doing crypto stuff, use the current time.

If the Random class is not good enough for you, I can't really
help. Random number generation is hard! From what you described above
I get the feeling you don't want random numbers, just "different"
sequences.

Cheers!
Shyamal
Jul 17 '05 #8

P: n/a
Yes you were right, i was recreating the random object each time. Silly
mistake =(

Seems to be working the way i want it too now, Thanks

Nick


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #9

P: n/a

"Nicholas Geraldi" <ni*********@taconic.net> wrote in message
news:3f**********@corp.newsgroups.com...
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 using to get random numbers was

Random rn = new Random(System.currentTimeMillis());

but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a for loop 200 times and only got 3 unique results)


What you describe is the creation of a Random number generator object.

Once you create a Random object, with appropriate seed, you use one of the
methods to get another random number.

Random rn = new Random(System.currentTimeMillis());
for(i=0;i<1000000;i++) println(rn.nextInt(100));

Should print 1000000 random numbers between 0 and 99.

If you run it again, once the system clock has changed, it will print a
different set of 1000000 numbers.

You can use other methods in the Random class to get other types of results.

-- glen
Jul 17 '05 #10

P: n/a
If you want truly random numbers, you should use a website dedicated
to such things, like random.org. There you can request through HTTP a
set of random numbers (up to 10000 i belive). This is really only
necessary in cryptological purposes, for all other not-so-sensitive
stuff you could use java.lang.Random
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.