Math.Random is not random enough

Asked

Viewed 49 times

-1

I noticed that if I use the Math.random with values from 0 to 10, it is very possible that the result will be repeated or generate an approximate value of the previous.

For example, I executed:

function random() {
    return Math.floor(Math.random() * 10);
}

and left:

Math.random não tão aleatório

If it exists, how do I generate even more random values?

  • 3

    Based on what do you claim that Math.Random() is not random enough? Strong claims like this call for strong evidence. What tests have you done, besides printing half a dozen samples? Which Javascript implementation are you testing and claim to be bad?

  • good, for what I’m trying to do, really it’s not, I put the function random upstairs for you to test. I’m not saying it’s bad, I just want to prevent repeated numbers from being displayed less frequently, because the lower the maximum number, the more likely it is to be repeated frequently

  • 2

    The way you did, it will only generate numbers between 0 and 9, so of course after a few runs you will get repeat, and the more times you run, the more chances you will repeat. What exactly do you want to do?

  • wanted to have a larger variation if it ran again

1 answer

1


As described: "...it is very possible that the result will be repeated or generate an approximate value of the previous one." There are explanations of why there is no way a computer can generate truly random numbers. And how the crease of numbers that exemplified is small, is expected a variation between them to be less, given that the chance of falling the same number after the previous one is still the same within 0 to 10.

You can increase their limit (by replacing the maximum value by 100) and make an implementation as specified in mozilla documentation: Math.floor(Math.random() * Math.floor(100)); to obtain a greater variation. In the mentioned documentation is also explained how to generate random numbers with greater security.

I recommend researching more about randomly generated numbers by computers.

I hope I’ve helped in some way.

  • I’ll take a look

  • So there’s nothing I can do... Anyway, thank you for clarifying. I even tried to use several randoms and make the average, but it seems that reduced the randomness even more.

  • 1

    I even agree with saying that "there are explanations of why there is no way for a computer to generate truly random numbers," but I don’t believe that this is related to the author’s problem (there is even a demonstrated problem, in fact). Edit: this is not a problem in the answer, I only left as an observation in the sense that the author of the question does not improperly associate the perception with the explanation of the link.

  • 1

    Yeah, @Bacco, you’re right. I inserted it only because I considered it pertinent to the subject matter addressed in the question, along with a demonstration of how to "decrease randomness" (and actually, there is no problem in fact reported in the question).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.