random numbers???

3 views
Skip to first unread message

Atom

unread,
Aug 6, 2011, 2:43:06 PM8/6/11
to jsGameSoup
Hello,

I've learned the random generator is deterministic but somehow it
doesn't give even sequence of numbers for me. The following code gave
me always the same number which is the middle of the given range:

var r = new SeedableRandom();

for(var i = 0; i<100; i++){
document.getElementById('debug').value += r.nextInt(1, 10) + ",";
}

The following code gives "5" one hundred times. If I change the range
to "1,50" instead, it always gives 25. Is this normal? I'm using
Firefox 3.6.18.

Cheers,

AG

Chris McCormick

unread,
Aug 7, 2011, 1:10:45 AM8/7/11
to jsgam...@googlegroups.com
Hi,

You are quite right, and this should not happen. The key is that the generator has not been seeded yet. If you seed it with some number, e.g. r.seed(42); then it behaves as expected.

What I have done is to update the code so that it is seeded with the current timestamp by default now. This means that the numbers won't be deterministic unless you supply a seed yourself, but they will now be random (instead of 5, 5, 5, 5, 5,....).

<http://jsgamesoup.net/tests/random-simple.html> (you'll need javascript console to see this one working)

<http://jsgamesoup.net/jsdocs/symbols/SeedableRandom.html>

Thanks for finding this bug!

Cheers,

Chris.

-------------------
http://mccormick.cx

Atom

unread,
Aug 7, 2011, 3:57:35 AM8/7/11
to jsGameSoup

> What I have done is to update the code so that it is seeded with the current timestamp by default now.
> This means that the numbers won't be deterministic unless you supply a seed yourself, but they will
> now be random (instead of 5, 5, 5, 5, 5,....).

Thanks, perfect! :-)

Atom
Reply all
Reply to author
Forward
0 new messages