views:

840

answers:

3

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?

A: 

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 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 when you look at this article, he explains that there was a seed being used:

http://codepunk.hardwar.org.uk/ajs13.htm

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.

James Black
There is no way you can change the seed for Math.random() in Javascript. It seeds it using the current time when the script starts executing.
Matt Greer
@Matt - I have always just passed in a value, I didn't realize it was being ignored.
James Black
A: 

Random number generators are really pseudo random number generators - ie they use a formula to calculate a stream of numbers that is practically random.

So, for the same initial input values (seed) you will get the same stream. So the trick is to seed the random number generator with a good actually random seed.

So you need to pass in a seed into random() somehow - you could use some kind of hashing of the current time, or any other data that you think has some kind of randomness (if you want it to be "securely random" - that is a whole other subject and probably covered somewhere else).

So use something like: Math.random(Date.getMilliseconds()) - might do closer to what you want.

Michael Neale
Hmm, according to the ECMA-262 spec, `Math.random()` doesn't take any parameters. Do you know which browser `Math.random(Date.getMilliseconds())` works in?
Xavi
@Xavi, you're right, the above code works because the argument passed to `Math.random` is just simply ignored... http://bclary.com/2004/11/07/#a-15.8.2.14
CMS
Tested with a fixed "seed", and both IE and Firefox still returned random values. Xavi is right, the parameter is just ignored.
Badaro
can `(Math.random() + new Date().getMilliseconds() / 1000) / 2` be considered as 'seeding'?
Lukman
A: 

You could use

#include <sys/time.h>

and then to get a range of seeds ranging from 0 to 999,999, use

gettimeofday(&tv, NULL);
srand(tv.tv_usec);

Then, to get the random number in the range 0 - 499, use

r = 500*((double)rand() /((double)(RAND_MAX)+(double)(1.0))));

or add 1 to this result to shift it to the range 1 - 500.

Good luck

John R Doner
Javascript question, not C/C++.
Badaro
I certainly would like to know why a down vote was applied to this answer. It is a complete description of a straightforward way to generate the random number range desired, which neither of the previous answers addressed.
John R Doner
Because the question asked how to generate a random number in JavaScript (in the context of writing a jQuery plugin), and your answer explained how to generate a random number in C or C++.I don’t think it’s possible to write jQuery plugins in C or C++. Great answer, just not for this question.
Paul D. Waite