JavaScript PRNG

LucyDuke

staring at the clock
Joined
Jan 21, 2007
Messages
13,583
Location
where mise
Okay, so I'm building a website for somebody, and I have a feature in mind that exceeds my (limited) JavaScript skills.

I need two things.

One - I need to define a variable as a number based on the year, month, and day. So the number needs to be the same all day, and a different number the next day, and a different number the following day, and so on. It shouldn't repeat each year. I know how to define a variable as a string of the date, but not as a number from the date. It needs to be a number so it can feed into...

Two - I need to use that variable to seed a "random" number between 1 and 100. I can't use the regular built-in RNG because this number needs to be consistent all day, too, and change every day. It can't be the same as the date number, though, it needs to be kind of randomized. I figure I can just use some erratic equation, but maybe somebody's got a good one already.

Can anybody help? :please:


EDIT: I've got the date part figured out. Now all I need is a good PRNG equation.
 
Assuming you have a number dependant on the day called 'seed', this should work:
Code:
var pseudoRand = parseInt((Math.sqrt(seed) * 1000000) % 100);

Not exactly elegant, I know, but it gives you a seemingly random number between 0 and 99. :)
 
Elegant? That looks beautiful. It's perfect, I think. Thank you! :)

Could you explain to me how it's working, too? The two things I don't recognize are "parseInt" and "%".


Oh, and this is how I'm generating my "seed". I took a script I found and busted it up to do what I want it to, which is the only way I can really make anything work in JavaScript. :undecide: Does it look all right?

var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900

var date=mydate.getDate()

var month=mydate.getMonth()+1

var year1=year*365

var month1=month*31

var seed=date+month1+year1
 
Well, let's assume we use the seed that your script would generate for today, which is 733216.

First, the square root is taken: 856.2803279300535
This is then multiplied by 1000000 (the number chosen here i fairly arbitrary) giving 856280327.9300535

The % (modulus symbol) then finds the remainder of 856280327.9300535 upon division by 100, which is equal to cutting off the first seven digits: 27.9300535

parseInt() basically interprets any possible datatype and turns it into an integer. When given a floating number, like ours, it simply rounds it down to the nearest integer. And there you have it. :)


Regarding your seed generator, it looks alright and it seems to be working as intended, though I don't understand why you're checking that the year is at least 1000? Seems redundant to me. Personally I would write the seed generator like this:
Code:
var myDate = new Date();
var seed = myDate.getDate() + myDate.getMonth() * 31 + myDate.getYear() * 365;
Hope that helps. :)
 
Awesome. Thank you. :)

Like I said, I just took a script someone else had written, and hacked it down to make it do what I needed from it. I didn't think the y2k fix was going to hurt anything, so I just left it alone. My skills are very limited!
 
The reason for the < 1000 check is some older browsers gave a 2 digit number which was fine untill 1999 (it gave 99) but then on 2k it gave 100 and today it would give 108.

I don't think any modern browsers stll have the problem but you may as well leave the check in.
 
Back
Top Bottom