Re: Math.random



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
.



Relevant Pages

  • Re: Agduria dungeon generation
    ... stage of the algorithm and rise appropriate error, ... A NTAE generator doesn't have to have any ... NTAE will happily accept all valid inputs and RNG states and produce ...
    (rec.games.roguelike.development)
  • I want to make a C# library of some classes
    ... I have a C++ class library (and application generator, ... member functions which return elements of those arrays. ... Note the current code is targeted to VS 2003, but I can target VS 2005 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: real random
    ... Feel free to post such an algorithm, ... it as a generator of a stream of numbers, ... You need a genuine source of entropy, ... By suggesting Fortuna (which gathers genuine entropy as it goes), ...
    (comp.lang.c)
  • Re: Agduria dungeon generation
    ... Depends entirely on the pseudo random number generator, ... algorithm don't suddenly change that probability. ... TAE isn't even obviously less ... algorithms but do understand their NTAE algorithms, ...
    (rec.games.roguelike.development)
  • Re: My cave room generator using wave functions
    ... for generating a generic cave room generator. ... algorithm, ... Randmonly choose n values to seed the wave function to be used. ... If the distance to the center is less than the radius of the room ...
    (rec.games.roguelike.development)