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

Re: 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:
>
>...
>
>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

I guess I should have posted this sooner. Sorry.

I believe this IS a psuedo-random number generator. I've been using it in
my Interstellar Strategy Game for a month now. Seems to work fine.

<SCRIPT>
function init(factor) {
        var today = new Date()
        return (factor*today.getTime()/1000)%30000
}
//Used to initialize rand() variables

IX = init(2)
IY = init(3)
IZ = init(4)
//2, 3, and 4 below were arbitrarily chosen.
//They put some distance between the initial values of IX, IY, and IZ

function rand() {
        IX = Math.floor(171 * IX % 30269)
        IY = Math.floor(172 * IY % 30307)
        IZ = Math.floor(170 * IZ % 30323)
        return ( IX/30269.0 + IY/30307.0 + IZ/30323.0 ) % 1.0
}
//Returns pseudo-random number between 0 and 1

//Random number function based on http://lib.stat.cmu.edu/griffiths-hill/183
</SCRIPT>


(BTW, it took me maybe 10 minutes to find the original algorithm using the
Alta Vista search engine. All praise Alta Vista!)



--------------------------------------------------------------------
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'.