I created a jQuery plugin but I have a problem, I use the following code:
Math.floor(Math.random()*500)
I add the result to a element,
but bizarrely the result is every time the same.
If I add a alert() to the line after the random-number-generation,
I get random values, why?
I want to get without the alert() random integers. But how?
The random number function is an equation that simulates being random, but, it is still a function. If you give it the same seed the first answer will be the same.
You could try changing the seed, and do this when the javascript is first loaded, so that if there is a time component to the random number generator, then it can use the delays of pages being loaded to randomize the numbers more.
But, you may want to change the seed. You can use the
Date()function, then get the milliseconds and use that as the seed, and that may help to scramble it up first.My thought that there is a time component to the generator is the fact that it changes with an alert, as that will delay when the next number is generated, though I haven’t tested this out.
UPDATE:
I realize the specification states that there is no parameter for Math.random, but there is a seed being used.
I came at this from C and then Java, so the fact that there was no error using an argument led me to think it used it, but now I see that that was incorrect.
If you really need a seed, your best bet is to write a random number generator, and then Knuth books are the best starting point for that.