Random Number generator

0 views
Skip to first unread message

Shuchi

unread,
Feb 5, 2010, 4:23:01 PM2/5/10
to EDUC 176 Programming Lab
Sorry to bother again. Can you share an algorithm to generate a random
number between 0 & 9, and one for 0 & 99..? Can we borrow from Steve's
balloon app?

Thanks,
Shuchi

Troy

unread,
Feb 5, 2010, 4:38:19 PM2/5/10
to EDUC 176 Programming Lab
To generate a random *float* between 0.0 and 0.9999999, use
Math.random().

To generate a random *float* between 0.0 and 9.999999, use
Math.random() * 10.

To generate a random *integer* between 0 and 9, use
Math.floor(Math.random() * 10).

For a full explanation of how this works, check out this explanation:
http://www.shawnolson.net/a/789/make-javascript-mathrandom-useful.html

Now, can you see how to generate numbers between 0 & 99 as well?

Finally, can you write a function that takes in a "max" parameter, and
then generates a number between 0 & max?

Hope this helps!

Troy

PS. No need to apologize. We're more than happy to help answer your
questions!

Shuchi Grover

unread,
Feb 5, 2010, 4:45:23 PM2/5/10
to educ-176-pro...@googlegroups.com
That's easy. Thanks :)
Reply all
Reply to author
Forward
0 new messages