Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 4 years ago.
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.
This is how I solved it for my needs. In my case it works just fine because I will only be requesting numbers sporadically and never sequentially or in a loop. This won't work if you use it inside a loop since it's time based and the loop will execute the requests just milliseconds apart.
function getRandomNumber(quantity_of_nums){
var milliseconds = new Date().getMilliseconds();
return Math.floor(milliseconds * quantity_of_nums / 1000);
}
This will give you a number from 0 to quantity_of_nums - 1
Hope it helps!
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.
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