Print

Print


Hi! ;)

java.lang.Math.random() returns a sequence generated by Java.util.Random.
The seed for random() is the value of System.currentTimeMillis() when java
is instantiated.  This seed will repeat far in the future, but more
important is the problem of randomness of currentTimeMillis().   Most
experts agree that the lowest order elements of currentTimeMillis() only
provide a few bits of entropy at best.  In fact over successive runs of your
program, you'll find currentTimeMillis is guaranteed to return "roughly" the
same value within a few percent margin.   This type of problem might cause
your program to have an affinity for certain subsequences of pseudo-random
values.

Random uses a "linear congruential sequence" of the form:

x_n = (a x_n-1 + c) mod m

with:
    private final static long multiplier = 0x5DEECE66DL;
    private final static long addend = 0xBL;
    private final static long mask = (1L << 48) - 1;

The linear congruential sequence was introduced by D. H. Lehmer in
Proc. 2nd Symp. on Large-scale Digital Calculating Machinery, Harvard
University Press, 1951, 141-146.   (With a title like that I couldn't resist
the citation)

See Donald Knuth, The Art of Computer Programming V2, for authoritative
coverage.  I can give you a few highlights.

According to Professor Knuth (p. 16) you can prove the period of this
sequence is m (281474976710655) iff
a. addend is relatively prime to m.
b. b = multiplier - 1 is a multiple of p, for every prime p dividing
m.
c. b is a multiple of 4, if m is a multiple of 4.

I didn't confirm the numbers used by java work, but feel free to have at it
;) If they don't you'll certainly have a nice bug to report to Sun.

Knuth also considers the strengths and weaknesses of this method at length
(150 pages...) but concludes the sequence is adequate and provides good
spectral coverage when appropriate constants are selected.

If you have on the order of >>billions of calls to random, I would highly
recommend using a cryptographic hash such as SHA (provided by Java using the
MessageDigest interface).  These types of cryptographic hashes are generally
very high quality but also somewhat expensive.

You might also consider reseeding more often using a decent source of
entropy such as /dev/random.    The class java.security.SecureRandom
provides this facility but is more expensive in the long run.

John
----- Original Message ----- 
From: "Brandon Drummond" <[log in to unmask]>
To: "lcd-sim" <[log in to unmask]>
Sent: Thursday, August 05, 2004 11:24 AM
Subject: random numbers


> Hello,
> This may, technically, be more of a java question, but I have a program
> written for jas that generates random numbers with the Math.random()
> function. I use this program to generate some output then make some
> changes to the source recompile and produce some more output, and I do
> this a number of times. I guess my question has to do with the random()
> function in java and how likely it is that I'll repeat a set of numbers
> or a seed. I don't pass a seed to the function so, right now, it just
> using whatever it uses by default. I know that the (capitol R) Random()
> function uses the difference in time between now and Jan. 1 1970 in
> milliseconds for a seed, but does anyone know what the (lower case r)
> random() function does? Should I consider using the Random() function
> instead?
>
> Thanks,
> Brandon Drummond
>
>