Math.random as far as I know returns pseudo-random numbers. One of my ideas for generating real random numbers was to pinpoint very small periods of time that would give an error. I tried to write this, but the truth was disappointed that the observed time values ​​were repeated very often and it was not like the generation of random numbers. Maybe you have some interesting ideas? The question is theoretical and is not of practical use, probably. Thank.

From Wikipedia: Sources of real random numbers are extremely difficult to find. Physical noise, such as detectors of ionizing radiation events, shot noise in a resistor, or cosmic radiation, can be such sources.

  • four
    since the program code as a whole is deterministic, it is impossible to write a generator of absolutely random numbers. But if you attract third-party tools (electronic generators) or download from external resources, then everything is possible. - KoVadim 6:09 pm
  • one
    @KoVadim I just thought, but what if I just get the millisecond end of the transaction using Date.now ()? Isn't this a true random number generator? - user208916 pm
  • 3
    If you need to get one number, then maybe it will be as if random (if we get it as a result of a user clicking on a button). But this is not a generator. But Date.now can produce quantized values ​​and it is quite possible that you will always get 03 or 98. I recommend that you take Knut's book, the second volume, the third chapter, and read it. Many questions will leave themselves. - KoVadim
  • For javascript, random.org is particularly suitable, atmospheric noise is used (if not lying). You can also collect the low bits of digitizing a signal from a microphone or webcam in a few seconds, and then use it as a grain for PRNG (although this will not always work as intended). - Vladimir Gamalyan

1 answer 1

A random number is a number that cannot be described. And you in the javascript code will describe it. This is contrary to the definition of a random number. But you can calculate a pseudo-random ("random type") number. How exactly - I do not know. And all the generators of pseudo-random numbers, including Math.random (), are implemented - they consider not a random number, but a difficultly predictable number.

As for the date - I myself made such a generator recently, I decided to write simple games in javascript. I had to run every time when I ran around the cells of the playing field to get a “type of random” number and, based on it, to conclude what exactly to do with the current cell of the playing field. Milliseconds instantaneous picked up, processed them (for example, took the remainder of the division by 2, that is, figured out an even number or an odd number millisecond in my hands). At first, the option seemed very cool to me, because I do not know how many milliseconds at a particular point in time it runs. But it turns out garbage in the end, and now I will explain why. The browser considers everything very quickly. If you push this "generator" code into a function and thus "generate" (calculate) "type of random" numbers in a row (that is, call this function in a row, call after call), the browser will make several passes through this function less than in 1 millisecond. You can try it yourself, and in the end you will get random numbers "in the same bundles", for example, 5 times an even number, 4 times an odd number .... The first option that I came up with was to load the browser with calculations inside the function so that it would count. I pushed the calculations of a huge factorial there, changing which, regulated the operation time of the function. But it was also necessary to regulate the probability of the events that this function generates. The probability of my event was about 1/2. To achieve 1/6, it was necessary to run the function 3 times with this factorial. In the end, yes, the function worked as I needed and gave me the probability I needed. But it takes a long time to run through all the cells of the playing field in this way - about a second. So no one does. After all, the browser is single-threaded. And I take it up for a full second. This is not an option. Everything else will slow down.

Another option that I did - you can translate the number of these milliseconds into a string and "get" each digit. And do something with it. But, all the same - the browser will issue a series of identical milliseconds. Which need to somehow weed out.

If you think well, then I think you can find an option. Now I came up with a slightly different option, I do not know whether it will work as it should. Every time I get a "0" or "1" generator, I stuff this thing into the array (i). Then again I get a pseudo random unit or zero (i + 1). And I throw random numbers into the array. Then I get the third number zero or 1 (i + 2). If all three of these numbers were in the same "party", which managed to miscalculate in less than 1 millisecond, then they will be the same. Take, for example, the second and third and delete. Only one number remains in the array. We run the function again and hammer three variables into the array again. If they are all the same, then the last two are deleted ... And so on. An array with random numbers "1" and "0" is clogged. Thus, I think you can reduce the repeatability of the same result in a series of function generator calls. In short, you just have to try.