Re: Math.random
- From: Dr J R Stockton <reply0925@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 21 Jun 2009 21:15:48 +0100
In comp.lang.javascript message <Xns9C316C85431EDeejj99@xxxxxxxxxxxxxxx>
, Sun, 21 Jun 2009 08:40:04, Evertjan. <exjxw.hannivoort@xxxxxxxxxxxx>
posted:
Dr J R Stockton wrote on 21 jun 2009 in comp.lang.javascript:
[..]
A repeatable seed is interesting however for code testing.
Indisputably.
That could be introduced in ECMA 5+ by simply saying that Math.random()
would do what it "always" has, but Math.random(Object) would use the
object to store the internal state, seed included. With different
Objects, one could have independent Random generators.
Something like this?
What I had in mind, and have implemented (apart from some necessary
polishing) in <URL:http://www.merlyn.demon.co.uk/js-randm.htm>, is not
greatly like your suggestions.
There, an algorithm of the form
X[n+1] = (X[n]*A + B) % C
is used, and the Object holds integers X A B & log2(C). Since X*A needs
to be exact, and a good A is likely to be of the order of log2(C-1), one
cannot using this and ordinary arithmetic make a reliable 32-bit
generator. Therefore, X A & B are now arrays of 16-bit integers (could
be 24, since 24+24<53), automatically giving the aforementioned
megadigit capability.
That's not the only possible algorithm; but AIUI it's generally
suitable, with well-chosen A B C, for non-crypto work. One could
without much difficulty implement a long shift-register generator; but
it is necessary to determine efficient taps (algorithm uses a very long
array of bits each cycle moves all bits one place to the left, and sets
the vacated LSB to the XOR of the old MSB and some of the other bits).
The function alters the value of X in the Object, and returns what
should be the result of normalising sign = +, mantissa = any 53-bit
integer, exponent is constant, such that the range is 0.0 <= R < 1.0.
One sets the initial X with either a chosen number or a random one
obtained somehow.
Math.random(myObject)
0..0.99999etc, where:
myObject.seed read/write,
doublevalue or null/undefined/false for auto reseeding,
0 for remember seed/step
myObject.step read/write,
integer step since last seeding, default 0 [= first step],
null/undefined/false or nextstep value for next step
cannot have value higher than next step [???]
Math.random()
Auto seeding on first call,
remember seed/step as in present version
Math.random(Null)
Auto reseeding
Math.random(-3.4,365.3)
Integer autoseeding, fast, easy implementation, perhaps not too
perfect, value interval includes endvalues
Math.random(365)
0... 365 included
I think 0 to 364 would be better; it goes well with zero-based arrays,
and matches other languages.
The core would be more easily implemented in another language, for
example one supporting 32-bit multiplication and 64-bit addition.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk BP7, Delphi 3 & 2006.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
NOT <URL:http://support.codegear.com/newsgroups/>: news:borland.* Guidelines
.
- Follow-Ups:
- Re: Math.random
- From: Evertjan.
- Re: Math.random
- References:
- Math.random
- From: Dr J R Stockton
- Re: Math.random
- From: Dr J R Stockton
- Re: Math.random
- From: Evertjan.
- Re: Math.random
- From: Dr J R Stockton
- Re: Math.random
- From: Evertjan.
- Math.random
- Prev by Date: Re: how to put in an HTML element the content of a txt file?
- Next by Date: Re: FAQ Topic - How do I get the value of a form control? (2009-06-19)
- Previous by thread: Re: Math.random
- Next by thread: Re: Math.random
- Index(es):
Relevant Pages
|