[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

until we get Math.random



In response to Gordon and Jirawat on the prisrand() function, I've cobbled
together a cludgy little random number generator for non-Unix Javascript
platforms. Give this a shot:

function rand() {
      today= new Date();
      ticks = (today.getTime()/1000);
      return (((ticks * 1103515245) % 499) / 498)
             }
//returns a vaguely random floating point number between 0 and 1

This is NOT a random number generator, or a psuedo-random number generator,
in the mathematical sense of the word. It's a cludgy hack designed to add a
little spice to javascript at this point in its development. I'm dividing
today.getTime() through by 1000 because on my Macintosh, the browser only
appears to report the time to the environment once per second. That means
that the time, reported in miliseconds, always ends in 000, which is
something that will not give you much variety when you're running it
through a mod statment. If someone could do a check on X11 or Windows and
tell me whether the browsers release time into the environment more than
once a second, I'd really appreciate it. Anyway, that really big number
(1103515245) is a prime - you can use any large prime there, but it's got
to be a lot larger than the second prime (499). Plug in your own primes and
check the behavior. (Obviously, divide through by the second prime minus
one...) If anyone's got a nice 12-digit prime sitting around, please email
it to me.

Hope someone finds this helpful.
-Ethan

Ethan Zuckerman                                         http://www.tripod.com
Webkeeper - Tripod Inc.                                        ethan@tripod.com
191 Water Street                                             413.458.2265 phone
Williamstown, MA 01267                                    413.458.2465 fax


--------------------------------------------------------------------
This message came from the mailing list javascript. For help using the
mailing list software, please send a message to 'majordomo@obscure.org'
with the message body 'help'. To unsubscribe, send a message to
'majordomo@obscure.org' with the message body 'unsubscribe javascript'.