Random selection of numbers. How random numbers are generated in a computer


  • Tutorial

Have you ever wondered how Math.random() works? What is a random number and how is it obtained? And present a question at the interview - write your generator random numbers in a couple of lines of code. And so, what is it, an accident and is it possible to predict it?

I am very fascinated by various IT puzzles and puzzles, and the random number generator is one of such puzzles. Usually in my telegram channel I sort out all sorts of puzzles and various tasks from interviews. The task about the random number generator has gained great popularity and I wanted to perpetuate it in the bowels of one of the authoritative sources of information - that is, here on Habré.

This material will be useful to all those front-end developers and Node.js developers who are at the forefront of technology and want to get into the blockchain project / startup, where questions about security and cryptography, at least on basic level, even front-end developers are asked.

Pseudo random number generator and random number generator

In order to get something random, we need a source of entropy, a source of some kind of chaos from which we will use to generate randomness.

This source is used to accumulate entropy, followed by obtaining from it the initial value (initial value, seed), which is necessary for random number generators (RNG) to generate random numbers.

The Pseudo-Random Number Generator uses a single seed value, hence its pseudo-randomness, while the Random Number Generator always generates a random number, starting with a high-quality random value that is taken from various sources of entropy.

Entropy - is a measure of disorder. Information entropy is a measure of the uncertainty or unpredictability of information.
It turns out that in order to create a pseudo-random sequence, we need an algorithm that will generate some sequence based on a certain formula. But such a sequence can be predicted. However, let's imagine how we could write our own random number generator if we didn't have Math.random()

PRNG has some algorithm that can be reproduced.
RNG - is getting numbers completely from any noise, the ability to calculate which tends to zero. At the same time, the RNG has certain algorithms for leveling the distribution.

Inventing our own PRNG algorithm

Generator pseudo-random numbers(PRNG, English pseudorandom number generator, PRNG) - an algorithm that generates a sequence of numbers, the elements of which are almost independent of each other and obey a given distribution (usually uniform).
We can take a sequence of some numbers and take the modulus of the number from them. The simplest example that comes to mind. We need to think about what sequence to take and the module from what. If just directly from 0 to N and module 2, then you get a generator of 1 and 0:

Function* rand() ( const n = 100; const mod = 2; let i = 0; while (true) ( ​​yield i % mod; if (i++ > n) i = 0; ) ) let i = 0; for (let x of rand()) ( if (i++ > 100) break; console.log(x); )
This function generates for us the sequence 01010101010101 ... and it cannot even be called pseudo-random. For a generator to be random, it must pass the test for the next bit. But we do not have such a task. Nevertheless, even without any tests, we can predict the next sequence, which means that such an algorithm is not suitable in the forehead, but we are in the right direction.

But what if we take some well-known, but non-linear sequence, for example, the number PI. And as a value for the module, we will take not 2, but something else. You can even think about the changing value of the module. The sequence of digits in Pi is considered random. The generator can work using pi starting from some unknown point. An example of such an algorithm, with a PI-based sequence and modulo change:

Const vector = [...Math.PI.toFixed(48).replace(".","")]; function* rand() ( for (let i=3; i<1000; i++) { if (i >99) i = 2; for (let n=0; n But in JS, the number PI can only be displayed up to 48 characters and no more. Therefore, it is still easy to predict such a sequence, and each run of such a generator will always produce the same numbers. But our generator has already begun to show numbers from 0 to 9.

We got a number generator from 0 to 9, but the distribution is very uneven and it will generate the same sequence every time.

We can take not the number Pi, but the time in numerical representation and consider this number as a sequence of digits, and in order to prevent the sequence from repeating each time, we will read it from the end. In total, our algorithm for our PRNG will look like this:

Function* rand() ( let newNumVector = () => [...(+new Date)+""].reverse(); let vector = newNumVector(); let i=2; while (true) ( ​​if ( i++ > 99) i = 2; let n=-1; while (++n< vector.length) yield (vector[n] % i); vector = newNumVector(); } } // TEST: let i = 0; for (let x of rand()) { if (i++ >100) break; console.log(x) )
Now it looks like a pseudo-random number generator. And the same Math.random() - is a PRNG, we'll talk about it a little later. Moreover, each time the first number is different.

Actually on these simple examples you can understand how more complex random number generators work. And there are even ready-made algorithms. For example, let's analyze one of them - this is the Linear Congruent PRNG (LCPRNG).

Linear congruent PRNG

Linear Congruential PRNG (LCPRNG) -  is a common method for generating pseudo-random numbers. It does not have cryptographic strength. This method consists in calculating the terms of a linear recurrent sequence modulo some natural number m given by the formula. The resulting sequence depends on the choice of the starting number - i.e. seed. At different meanings seed yields different sequences of random numbers. An example of the implementation of such an algorithm in JavaScript:

Const a = 45; const c = 21; const m = 67; varseed = 2; const rand = () => seed = (a * seed + c) % m; for(let i=0; i<30; i++) console.log(rand())
Many programming languages ​​use LCPRNG (but not just such an algorithm (!).

As mentioned above, such a sequence can be predicted. So why do we need PRNG? If we talk about security, then PRNG is a problem. If we talk about other tasks, then these properties  -  can play a plus. For example, for various special effects and graphics animations, you may need to call random frequently. And here the distribution of values ​​​​and performance are important! Security algorithms cannot boast of speed.

Another property - reproducibility. Some implementations allow you to specify a seed, which is very useful if a sequence is to be repeated. Reproduction is necessary in tests, for example. And there are many other things that do not require a secure RNG.

How Math.random() works

The Math.random() method returns a pseudo-random floating point number from the range = crypto.getRandomValues(new Uint8Array(1)); console log(rvalue)
But, unlike PRNG Math.random(), this method is very resource intensive. The fact is that this generator uses system calls in the OS to access entropy sources (poppy address, cpu, temperature, etc ...).

It is an obvious fact that luck plays an important role in any undertaking. But when playing the lottery, you need to understand that fortune is the only factor on which the fulfillment of your dreams depends. In the majority of lotteries, in order to get the jackpot, you just need to guess certain numbers in a certain range. In this case, the one presented on our website is able to help.

We offer to try a simple generator for free, which is able to completely eliminate the influence of the human factor and increase the chance of winning. We also provide the best and most functional, but simple generators, as well as services that can predict winning combinations of numbers based on special analysis algorithms.

If you want to try your luck in one of the popular lotteries (4 out of 20, 5 out of 36, 6 out of 45), but don't know which numbers can increase your chances of winning, then we can help. Next, we bring to your attention overview of TOP 5 most functional, but at the same time easy-to-use lottery number generators with many additional features and capabilities.

First, let's analyze the main criteria of the above list.:

TOP 1 - Number Generator GSgen.RU


Description: The embedded software is implemented in the Javascript programming language and is a pseudo-random number generator. Evenly distributes random numbers, thereby eliminating the subjective perception of the players, which affects the manual selection.

Advantages: The RNG script allows you to select lucky numbers for Gosloto (and not only) of various variations from preset modes. There is a possibility of individual settings for other types of lotteries. Available for free use.

Flaws: It is not possible to enter exclusion numbers that are not desirable to see, you cannot get several combinations at once and get a link to the finished result.

TOP 2 - Generator Soft-Arhiv


Description: Another service for generating midrange for Russian lotteries. It is enough to choose the necessary combination and get the finished result. No additional software is required for use, as it works great online.

Advantages: Has a simple, visual form for filling out and receiving results. The ability to select a ready-made lottery type, generation settings allows you to enable exceptions and the number of required combinations make the service very convenient to use. Also completely free functionality.

TOP 3 - RNG: Calculator888


Description: Calculator888 is in the honorable third place of the considered services. Like the previous options, it allows you to get the required number of numbers without much effort. Even a novice network user can use the random number generator, since everything is intuitive.

Advantages: Wide settings will allow you to generate the required number of numbers, set their range, and also define input options. In addition, unlike previous services, it allows you to get a link to the result. Completely free.

Flaws: The disadvantages include the lack of a choice of ready-made types of lottery, which forces you to independently form the task. You cannot introduce exceptions and get several combinations at once. Accounting for past runs is also not made.

Number generators based on past draws

It is worth paying your attention to the fact that there are special services that can predict lucky numbers that you need to bet on. Their creators assure users that the analysis and issuance of results is carried out on the basis of the results of the runs, the use probability theory and other mathematical calculations.

However, you should not unconditionally believe in it. We definitely do not believe in this and believe that any of these services are those that randomly give a result similar to any other RNG.

However, you can check it yourself. Next, we present two more services that provide the ability to select results for state lotto, viking loto, keno, sports lotto, etc. taking into account their past circulations. The functionality of some of them is paid.

Let us draw your attention to the fact that it is not worth paying money for a paid forecast, as it is just money down the drain for combinations that any other free service can give out. So, the continuation of the best services for generation, taking into account the analysis of past runs:

TOP 4 - RNG with circulation: Fortunablog


Description: According to the developer, the script is able not only to give out digital combinations at random, but also analyzes previously dropped balls based on a number of algorithms and probability theory. It is also stated that the purpose of the generator is to select a combination for the jackpot.

Advantages A: There are two preset lottery types that you can try your luck in picking. However, the main advantage is positioned as taking into account the results of past runs and, importantly, free use.

TOP 5 - Lottery Generator Based on Circulations: Igraivloto


Description: The presented service allows you to get combinations of the most likely winning combinations. The principle of operation is similar to the previous options considered, with the exception of certain points of functionality.

Advantages: It is a ready-made script for issuing a forecast for the Gosloto 6 out of 45 lottery, which eliminates the need to select the required draw. It positions itself as a site that works on special algorithms and filters that create the most probable forecasts based on the runs. Allows you to get multiple results at once and share a link to the result.

Flaws: It is not possible to enter a range of numbers and necessary exceptions. However, the biggest disadvantage is the provision of paid forecasts, which clearly distinguishes it from the free previous analogues.

Conclusion

It is up to you, of course, to use the considered services or not. On the one hand, the use of such sites can help you choose certain combinations, freeing you from a difficult choice, since, for example, for drawing 5 out of 36, absolutely any combination generated or manually selected by the player himself has a probability of winning 1 to 376,992.

Table showing the probability of winning the lottery:


Compared to other lottery strategies, this option has good chances of winning. However, it should be understood that using paid predictions in this case is not appropriate and does not correspond to the probability of winning.

The Random Number Generator for Lottery Tickets is provided free of charge on an "as is" basis. The developer does not bear any responsibility for the material and non-material losses of the users of the script. You may use this service at your own risk. However, something, but you definitely don’t take risks :-).

Random numbers for online lottery tickets

This software (PRNG in JS) is a pseudo-random number generator implemented with the capabilities of the Javascript programming language. The generator produces a uniform distribution of random numbers.

This allows the lottery company to beat out a “wedge with a wedge” on an evenly distributed RNG from a lottery company to respond with random numbers with a uniform distribution. This approach eliminates the subjectivity of the player, since people have certain preferences in choosing numbers and numbers (birthdays of relatives, memorable dates, years, etc.), which affect the selection of numbers manually.

The free tool helps players to pick random numbers for lotteries. The random number generator script has a set of preset modes for Gosloto 5 out of 36, 6 out of 45, 7 out of 49, 4 out of 20, Sportloto 6 out of 49. You can choose a random number generation mode with free settings for other lottery options.

Lottery winning predictions

A random number generator with a uniform distribution can serve as a horoscope for the lottery, however, the probability that the forecast will come true is low. But still, using a random number generator has a good chance of winning compared to many other lottery game strategies and additionally frees you from the pain of choosing lucky numbers and combinations. For my part, I do not advise you to succumb to the temptation and buy paid forecasts, it is better to spend this money on a textbook on combinatorics. You can learn a lot of interesting things from it, for example, the probability of winning the jackpot in Gosloto is 5 out of 36 1 to 376 992 . And the probability of getting the minimum prize by guessing 2 numbers is 1 to 8 . The forecast based on our RNG has the same winning probabilities.

On the Internet, there are requests for random numbers for the lottery, taking into account past draws. But provided that the lottery uses an RNG with a uniform distribution and the probability of getting one or another combination does not depend on the draw to the draw, then it is pointless to try to take into account the results of past draws. And this is quite logical, since it is not profitable for lottery companies to allow participants to increase the probability of their winning by simple methods.

There is often talk that lottery organizers rig the results. But in fact, this makes no sense, even, on the contrary, if lottery companies influenced the results of the lottery, then it would be possible to find a winning strategy, but so far no one has succeeded. Therefore, it is very beneficial for lottery organizers that the balls fall out with a uniform probability. By the way, the estimated return of the lottery 5 out of 36 is 34.7%. Thus, the lottery company has 65.3% of the proceeds from ticket sales, part of the funds (usually half) is deducted for the formation of the jackpot, the rest of the money goes to organizational expenses, advertising and the company's net profit. The circulation statistics confirm these figures perfectly.

Hence the conclusion - do not buy meaningless forecasts, use a free random number generator, take care of your nerves. Let our random numbers be your lucky numbers. Good mood and good day!

Please help the service with one click: Tell your friends about the generator!

Number generator online in 1 click

The random number generator, which is presented on our website, is very convenient. For example, it can be used in drawings and lotteries to determine the winner. Winners are determined in this way: the program gives out one or more numbers in any range you specify. The manipulation of the results can be immediately eliminated. And thanks to this, the winner is determined in a fair choice.

Sometimes you need to get a certain number of random numbers at once. For example, you want to fill out a “4 out of 35” lottery ticket, trusting in chance. You can check: if you toss a coin 32 times, what is the probability that 10 reverses will fall out in a row (heads / tails may well be assigned by the numbers 0 and 1)?

Random number online video instruction - randomizer

Our number generator is very easy to use. It does not require downloading a program to a computer - it can be used online. To get the number you need, you need to set the range of random numbers, the number and, if desired, the number separator and exclude repetitions.

To generate random numbers in a specific frequency range:

  • Choose a range;
  • Specify the number of random numbers;
  • The "Number separator" function serves for the beauty and convenience of their display;
  • If necessary, enable / disable repetitions with a checkmark;
  • Click the "Generate" button.

As a result, you will receive random numbers in a given range. The result of the number generator can be copied or sent to e-mail. It would be best to take a screenshot or video of this generation process. Our randomizer will solve any of your problems!

With this generator you will be able to generate random numbers in any range. This generator will also allow you to randomly select or determine a number from a list. Or create an array of random numbers from 2 to 70 elements. This online tool will not only allow you to generate one (1), two (2) or three (3) digit random numbers, but also five and seven. Easy to set up. Everyone can master it. You will also be able to choose random numbers for online or offline lotteries or contests. And it will be convenient. You can easily create entire tables or rows of random numbers. In a fraction of a second, you will receive a random number or their sequence (set) on your screen. If you take a sequence of your numbers, then the algorithm will choose a random one or random ones, any one can fall out. You yourself can use this tool to conduct draws. By choosing, for example, the same range and number of numbers as a result, you can generate a random sequence (combination). You can also choose random letter combinations and words. This tool, like everything on our site, is absolutely free to use (no exceptions).

Enter range numbers

From
Before
To generate

Changing the Range to Generate a Random Number

1..10 1..100 1..1000 1..10000 for lottery 5 out of 36 for lottery 6 out of 45 for lottery 6 out of 49 for lottery 6 out of 59

Number of random numbers (1)

Eliminate repetitions

Select random values ​​from the list (separate by commas or spaces, if commas are found, the division will be made by them, otherwise by spaces)

Editor's Choice
Fish is a source of nutrients necessary for the life of the human body. It can be salted, smoked,...

Elements of Eastern symbolism, Mantras, mudras, what do mandalas do? How to work with a mandala? Skillful application of the sound codes of mantras can...

Modern tool Where to start Burning methods Instruction for beginners Decorative wood burning is an art, ...

The formula and algorithm for calculating the specific gravity in percent There is a set (whole), which includes several components (composite ...
Animal husbandry is a branch of agriculture that specializes in breeding domestic animals. The main purpose of the industry is...
Market share of a company How to calculate a company's market share in practice? This question is often asked by beginner marketers. However,...
First mode (wave) The first wave (1785-1835) formed a technological mode based on new technologies in textile...
§one. General data Recall: sentences are divided into two-part, the grammatical basis of which consists of two main members - ...
The Great Soviet Encyclopedia gives the following definition of the concept of a dialect (from the Greek diblektos - conversation, dialect, dialect) - this is ...