LISTSERV mailing list manager LISTSERV 16.5

Help for LCD-SIM Archives


LCD-SIM Archives

LCD-SIM Archives


LCD-SIM@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

LCD-SIM Home

LCD-SIM Home

LCD-SIM  August 2004

LCD-SIM August 2004

Subject:

Re: random numbers

From:

"John Cairns" <[log in to unmask]>

Date:

9 Aug 2004 14:27:39 -0400Mon, 9 Aug 2004 14:27:39 -0400

Content-Type:

multipart/mixed

Parts/Attachments:

Parts/Attachments

text/plain (81 lines) , Random.java (81 lines)

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
>
>

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

May 2015
March 2015
February 2015
January 2013
November 2012
June 2012
July 2011
April 2011
September 2010
June 2010
February 2010
October 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
September 2006
August 2006
July 2006
April 2006
March 2006
February 2006
January 2006
December 2005
November 2005
October 2005
August 2005
July 2005
June 2005
May 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
April 2003
March 2003
February 2003
January 2003
July 2002
October 2001
June 2001
May 2001
March 2001
February 2001
December 2000
November 2000
October 2000
June 2000
April 2000
March 2000
February 2000
January 2000
November 1999
October 1999
September 1999
August 1999
July 1999
June 1999
May 1999
April 1999
March 1999
February 1999
January 1999

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use