How does Math.random() work?

6,021 views
Skip to first unread message

Guard13007

unread,
Sep 13, 2012, 1:36:58 PM9/13/12
to twee...@googlegroups.com
I am having some trouble working with the random function (it is called a function, right?). It seems to be acting the exact same way every time I follow the same set of actions, instead of being random each time (although this could just be my luck/failures because I have been having issues with my own competence this whole time).

My two questions:
- Does it get the seed for however it determines random numbers from your actions?
- Am I doing this wrong?

<<set $chance = Math.round(Math.random() + 1)>>
I am expecting either a 1 or a 2 from that. Am I doing it right? Also, does the random function tend to go for lower numbers over higher numbers? I seem to be hitting 1 a lot more often than 2 (but then again, this has started to balance out because now I am hitting 2 more often xD).
<<set $chance = Math.round(Math.random() * 2 + 1)>>
I am expecting this to return either a 1, 2, or 3. Again, am I doing this right? Multiplying by a number makes it go between 0 and that number (minus 1 because it starts at 0). Adding 1 will make it 1 to the multiplied number instead of 0 to the number before, right? Or have I gotten these completely wrong or backwards or something?

Thanks for the assistance whoever is out there. ^^

M. C. DeMarco

unread,
Sep 13, 2012, 9:20:56 PM9/13/12
to twee...@googlegroups.com
You might want Math.floor instead of round. For 1, 2, or 3:

Math.floor((Math.random()*3)+1);

See here:
--
You received this message because you are subscribed to the Google Groups "Tweecode / Twine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/tweecode/-/paqQ_Gp2TJkJ.
To post to this group, send email to twee...@googlegroups.com.
To unsubscribe from this group, send email to tweecode+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/tweecode?hl=en.


--
___________________
mcde...@gmail.com
http://gplus.to/mcdemarco/about

Guard13007

unread,
Sep 13, 2012, 9:45:29 PM9/13/12
to twee...@googlegroups.com
Ah, thanks.

And I looked up why you're using Math.floor instead of Math.round, it makes sense now. ^^ Cheers.

(For anyone finding this who doesn't know..if that happens: http://www.thonky.com/random-number/ )

Melonshead

unread,
Nov 4, 2012, 3:52:01 PM11/4/12
to twee...@googlegroups.com
Hello,

Since random is an issue of mine, and I am investigating the issue, I thought I post this here, if for no other reason than to have it stored in a place where I can find it again. After doing some reading, it appears there use to be an issue with this math.random thing and my Java Script book says there are other, better solutions. I myself don't want a one or two, or to send someone to another set place, so as to make "random" really pre-destinational. So the book recommended some Cornell guy named Paul, who wrote this script based on the clock time (I think). So for what it is worth, in understanding, here is the link: http://www.honeylocust.com/javascript/randomizer.html.

From there it seems this Paul built what he calls a "high-speed research-grade random number package in Java," which (he says) is "faster than math.random" and he shows benchmarks there for us to see. This high-speed Java package is available here:
http://www.honeylocust.com/RngPack/

How to use the code and where to put it, I don't know, but at least it adds to the understanding level.

Melanie

Stormrose

unread,
Nov 5, 2012, 9:39:57 AM11/5/12
to twee...@googlegroups.com
Interesting topic. Math.random() is only ever "pseudo random" - which is random enough for most purposes. It is automatically seeded using the clock in most cases. I use quite a bit of Math.random() in my Twine stories and it works well enough. I'm not securing user data with it or shuffling cards for high stakes poker.

This talks about predication attacks against Math.random()
Some of those attacks look (at a cursory glance) capable of predicting the output of the Pauls rnd() function.

Here's some better ones:

Last I checked, Chrome used only 32bits of entropy and davidbau's seedrandom() uses 256bits. Cool topic.

--Et
Reply all
Reply to author
Forward
0 new messages