Math.random () - Not by chance

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 the element, but the bizarre result is the same every time.

If I add a warning () to the string after generating random numbers, Why do I get random values? I want to get random integers without warning (). But how?

0
source share
4 answers

A random number function is an equation that mimics randomness, but it is still a function. If you give him the same seed, the first answer will be the same.

You can try changing the seed and do this when javascript is loaded first, so if there is a time component in the random number generator, it can use the delay of the loaded pages 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 it as a seed, and this can help to scramble it first.

My idea that there is a time component in the generator is that it changes with a warning, as it lingers on creating the next number, although I have not tested it.

UPDATE:

I understand that the specification states that there is no parameter for Math.random, but the seed is used.

I came to this with C, and then in Java, so the fact that there was no error using the argument made me think that he used it, but now I see that this is not true.

If you really need seed, it's best to write a random number generator, and then Knut's books are the best starting point for this.

+1
source

This is how I decided it for my needs. In my case, this works just fine, because I will be asking for numbers sporadically and never sequentially or in a loop. This will not work if you use it inside the loop, since it is time-based, and the loop will execute queries in just milliseconds.

 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 this helps!

0
source

Random number generators are really pseudo random number generators, i.e. use a formula to calculate a stream of numbers, which is almost random.

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

So, you need to pass the seed to random () somehow - you can 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 "safe โ€œrandomโ€ is a whole different subject and is probably covered elsewhere).

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

-5
source

you can use

 #include <sys/time.h> 

and then to get the seed range from 0 to 999,999, use

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

Then, to get a 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 move it to the range 1 - 500.

Good luck.

-thirteen
source

Source: https://habr.com/ru/post/888307/


All Articles