Application for random number selection. Random number generator for lottery

  • 10.05.2019
  • Tutorial

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

I am very fascinated by various IT puzzles and tasks, and the random number generator is one of these tasks. Usually in my Telegram channel I analyze all sorts of puzzles and various tasks from interviews. The random number generator problem has gained great popularity and I wanted to perpetuate it in the depths of one of the authoritative sources of information - that is, here on Habré.

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

Pseudo-random number generator and random number generator

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

This source is used to accumulate entropy and then obtain from it an initial value (seed), which is necessary for random number generators (RNG) to generate random numbers.

The Pseudo-Random Number Generator uses a single seed, hence its pseudo-randomness, while the Random Number Generator always generates a random number by starting with a high-quality random variable that is drawn 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 a certain 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 the process of obtaining numbers entirely from some kind of noise, the ability to calculate which tends to zero. At the same time, the RNG has certain algorithms for equalizing the distribution.

We come up with our own PRNG algorithm

Pseudorandom number generator (PRNG) is an algorithm that generates a sequence of numbers whose elements 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 which sequence to take and the module from what. If you just directly from 0 to N and modulus 2, 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 the sequence 01010101010101... and it cannot even be called pseudo-random. For a generator to be random, it must pass the next bit test. But we don’t have such a task. Nevertheless, even without any tests we can predict the next sequence, which means that such an algorithm is not suitable, but we are in the right direction.

What if we take some well-known but non-linear sequence, for example the number PI. And as the 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 operate using Pi numbers starting from some unknown point. An example of such an algorithm, with a PI-based sequence and a variable module:

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 PI number can only be displayed up to 48 digits 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 started showing numbers from 0 to 9.

We can take not the number Pi, but time in numerical representation and consider this number as a sequence of numbers, and in order to ensure that the sequence does not repeat 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)

This already 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 we get a different first number. Actually on these simple examples You can understand how more complex random number generators work.

And there are even ready-made algorithms. As an example, let’s look at one of them — this is the Linear Congruent PRNG (LCPRNG).

Linear congruent PRNG Linear congruent PRNG (LCPRNG) is a common method for generating pseudorandom numbers. It is not cryptographically strong. This method consists of 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 starting number — i.e. seed. At different meanings

seed produces different sequences of random numbers. An example of implementing such an algorithm in JavaScript:<30; i++) console.log(rand())
Const a = 45; const c = 21; const m = 67; var seed = 2; const rand = () => seed = (a * seed + c) % m; for(let i=0; i

Many programming languages ​​use LCPRNG (but not exactly this 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 be a plus. For example, for various special effects and graphics animations, you may need to frequently call random. And this is where the distribution of meanings and performance are important! Secure algorithms cannot boast of speed.

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

How Math.random() works
But, unlike the Math.random() PRNG, this method is very resource-intensive. The fact is that this generator uses system calls in the OS to gain access to entropy sources (mac address, CPU, temperature, etc...).

An online number generator is a convenient tool that allows you to get the required number of numbers of a given bit depth and the widest range. Our random number generator has many uses! For example, you can hold a competition on VKontakte and play there for a teddy bear in a group of bikers for a riposte :)) We will also be very flattered if, with the help of it, you decide to determine the winning number in any lottery or decide what number to bet on in a casino . We really hope that someone will find their lucky number online with us!

Random number range:

Quantity:

Eliminate repetition?

Generate numbers

Please help us develop: Tell your friends about the generator!

Random | random number online in 1 click

Numbers surround us from birth and play an important role in life. For many people, their work itself is connected with numbers; some rely on luck, filling out lottery tickets with numbers, while others attach even mystical meaning to them. One way or another, sometimes we cannot do without using a program such as random number generator.

For example, you need to organize a prize draw among your group’s subscribers. Our online random number generator will help you quickly and honestly select winners. You just need, for example, to set the required number of random numbers (based on the number of winners) and the maximum range (based on the number of participants, if numbers are assigned to them). Fraud in this case is completely excluded.

This program can also serve as a random number generator for lotto. For example, you bought a ticket and want to rely entirely on chance and luck in choosing the numbers. Then our number randomizer will help you fill out your lottery ticket.

How to generate a random number: instructions

Random number program It works very simply. You don't even need to download it to your computer - everything is done in the browser window where this page is open. Random numbers are generated in accordance with the specified number of numbers and their range - from 0 to 999999999. To generate a number online, you must:

  1. Select the range in which you want the result. Perhaps you want to cut off numbers up to 10 or, say, 10,000;
  2. Eliminate repetitions - by selecting this item, you will force number randomizer offer you only unique combinations within a certain range;
  3. Select the number of numbers – from 1 to 99999;
  4. Click the “Generate numbers” button.

No matter how many numbers you want to get as a result, the prime number generator will produce the entire result at once and you can see it on this page by scrolling through the field with numbers using the mouse or touchpad.

Now you can use the ready-made numbers the way you need. From the number field, you can copy the result to publish in a group or send by mail. And so that the result does not raise any doubts, take a screenshot of this page, on which the parameters of the number randomizer and the results of the program will be clearly visible. It is impossible to change the numbers in the field, so the possibility of manipulation is excluded. We hope our website and random number generator helped you.


Rating: 4.0 out of 5
Votes: 143
Random number generator for lotteries



1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
41 42 43 44 45
46 47 48 49


Numbers are exceptions
(separated by commas!)

*These numbers will not be used to generate the result.
Enter your numbers, or clear the field.

Generate options at a time (1-20)

The program is an online random number generator for Russian lotteries 5 out of 36, 6 out of 45, 7 out of 49, 6 out of 49. In addition to the number generator, such a useful tool as “Number Exceptions” is included.
Are you unlucky with the number 7 or 10? Then you can simply add these numbers to exceptions, and they will not be taken into account when generating numeric options.

Main features of the program
- Convenient, simple and visual interface.
- Customizable number generator: exception field, the number of generated combinations is adjustable from 1 to 20.
- Does not require installation. It will work on any device with Internet access.
- Works correctly with all popular browsers: Internet Explorer, Opera, Google Chrome and Mozilla Firefox.

System requirements
Any browser that supports the HTML5 standard

Please report any errors found or suggestions for improving the program in the comments. If you liked this number generator, please share the link to it on social networks or online forums.
We wish you good luck and good winnings in the lottery! We hope this program will help you with this.




Additional Information
License: For free
Software Developer: Soft-Archive
Supported OS: Windows XP, Windows Vista, Windows 7, Windows 8
Interface language: Russian
Update date: 2019-02-12


Comments and reviews: 35

1. Sergius 01.06.2014
Of course, I understand that gambling addicts are superstitious people, but I’m just wondering, what difference does it make: I come up with these numbers myself, or this number generator gives them to me?

2. Max 04.06.2014
Sergius, of course you can come up with numbers yourself. But when composing them, you will still be subject to a certain sequence, which will be influenced by factors such as favorite numbers, or just a number spinning in your head. That is, the numbers you come up with will be conditionally random.

The computer program is completely free from third-party interference and generates truly random numbers.

3.Iloinor 17.06.2014
When drawing 5 out of 36 balls in the same lottery, the balls are released randomly from the lottery drum. And their combination can be absolutely any. So it is simply impossible to generate a more or less successful combination. Any combination of numbers will always have the same winning ratio.
Who thinks differently?

4. Alexander 08.07.2014
Absolutely any one generated or compiled manually by the player himself has a probability of 1 in 376,992 (for lottery 5-36). In theory, this is possible! Those who think long enough about the problem of “how to increase the probability” will not agree with me.

And I came to the conclusion that everything is really hopeless. If you look at how combinations play in the full array of the same 5 out of 36, you can see that the combinations play with equal probability over a fairly large period of time.

At the same time, clusters seem to be observed (they looked at the starry sky), because there is also a random distribution there. We see that the stars cluster in certain places, but if we look through a telescope, the equally probable distribution remains.

Let’s go back to lotteries, if you look at such a map (of the combinations played), you can see that some areas “seemed to have quieted down,” and it’s these narrow ranges that become more likely than others for the coming games. Since, according to the law of equiprobable distribution, this area should be filled in the very near future. It makes sense to wait for combinations there. Our likelihood increases dramatically. We have a strategy that is aimed at railway sweat. This is a purposeful game, not blind throwing.

This is where special programs come in handy.
Contact the author of the random number generator displayed here. It can offer a special visualized program for the game + built-in strategy.

6. Pashka 02.01.2015
“Of course, I understand that gambling addicts are superstitious people.”

Not that word. My uncle always rubs all purchased Russian Lotto tickets on the sleeve of his lucky old jacket.

7. Samurai 06.01.2015
Do you want to win a Million in Lotto!? Do you want to know the secret to winning and the strategy for choosing the right numbers? You will find all the secrets of how to win the lotto on the website *moderator* loto.html
Play and win.

9. Nikolay 25.10.2015
Chance and luck speak. Of course, who can argue.
Have you imagined the number of combinations, for example, in the 6 out of 45 lottery?
If you clearly and clearly imagine this quantity, it will become obvious that it is inappropriate to rely only on chance and luck.
Just use your imagination a little, I hope you are not going to argue that we can use natural cunning and simply randomly exclude one single number out of 45.
At the same time, you need to try really hard so as not to snag the prize money. The chance of such an event will be 1 in 7.5.
Now we count - we have successfully excluded this number, in this case we have not 8,145,060 combinations left for the game, but 7,059,052... that is, we have reduced 1,086,008 (more than a million combinations) from the range of possible combinations with one single number.
This simple example illustrates the meaning of exceptions. And you don’t need to think that people who have devoted considerable time to studying methods of playing numerical lotteries write nothing but “vomit”.
- everything is mathematically justified.
Of course, Luck plays an important role in numerical lotteries, since we bet on a very small number of combinations for the game.
Therefore, in order to make it easier for “Luck” to find you, you need to use some gaming methods that are designed to PROBABLY reduce as many combinations as possible from the full array of the selected lottery.

10. Igor CK 03.09.2016
Nikolai wrote above about excluding one number in order to increase the chances that the remaining numbers will appear. In theory, all this is true! If, say, you exclude not 1, but 3 numbers, then the chances will increase even higher.
BUT there is one BUT! This is a lottery, everything is random and unpredictable. The same number may appear 10 times in a row, but another number may not appear even in 100 variations! It is impossible to calculate these very numbers, that's the point.

I remember back when I was studying at the university, our higher mathematics teacher, a pleasant and smart guy, talked about lotteries and accidents. So he said that it is impossible to create any systems or methods here in principle! The result is completely random and unpredictable.

I saw several paid programs and training methods on the Internet that “help” to create the necessary combinations of numbers that increase the chances of winning. You know what I'm curious about? If there is a way to increase the chances of winning, then why don’t those who sell them make money from lotteries? Yes, you won’t be able to hit the jackpot, the probability is too low, but you can win small amounts. Isn't it logical?
Of course, they may object to me - they say, one does not interfere with the other - making money on lotteries and selling techniques. But the fact is that if everyone uses these methods, provided, of course, that they actually work, then this will reduce the income from winnings for their creators, since they will have to be divided among a large number of people.

It’s like finding a hole in the Webmoney system that allows you to replenish your wallet with money “out of nowhere” and put this method up for sale so that it will be closed as soon as possible.

11. home 04.09.2016
Igor CK, what Nikolay wrote there - he wrote about one number, and the chances of not getting the prize money.
Next, consider what chances there will be if you exclude the 2nd number of not catching the future prize money, and so on))

Naturally, we cannot exclude them indefinitely; fantasy and fairy tales do not exist in lotteries, unless on fairy-tale sites that catch “seekers”))
A different approach is needed here; you need to follow not the numbers, but the periods that these numbers form.
Well, then build a strategy and get attached to the circulation history.

I decided to make a version of the generator for the mass user, and I’ll upload it for moderation today tomorrow.
On my website, I will open the page of this generator, and there I will try to outline a game strategy using the periodicity of complete and partial matches.
Winning the number lottery is difficult, but it is possible.

12. home 13.11.2016
In general, I wrote the basics on the website, which can be found by searching: “VISUAL GENERATOR - random number generator with exception.” Paid a lot of attention to probabilities.
I made a version for this strategy game, which can be downloaded on the website, or here - VISUAL LOTTO TESTER 3.1

13. Timofei 26.11.2016
A friend of mine from work won 63 thousand rubles in the lottery. He walks around happy like a boa constrictor. And I'm not having any luck at all. If you are lucky enough to win something, it will be just one small thing.

14. Max 26.11.2016
Guys, there is a wonderful program “Eurolotto winning generator for all lotteries in the world” - there are algorithms for calculating draws, yesterday I won 15,000 rubles and completely recovered the cost and also earned money!

15. Yuri 01.02.2017
Let's try to play and see what happens.

16. Alexander 04.06.2017
Not long ago I read in a live journal (I don’t remember exactly the address of the diary) analytical calculations about lotteries in Russia. The point is that the results of large wins are manipulated and those who play are shown pre-calculated combinations. In general, there is no threat of a jackpot for you and me.

Information is based on calculations of odds of winning, number of participants in the drawing and number of winnings. So, if you take the number of participants and calculate the chance of winning the jackpot, you get a huge gap between chance and reality.

If, for example, you take a random number generator and guess any number from 1 to 10, then your chance of guessing is 1 in 10. In Russian lotteries, with the same scheme, the chance of a big win is 1 in 40-50. And it is still unknown how real the person who wins the jackpot is.

17. home 04.06.2017
Pseudo-analytical mathematicians are spreading complete nonsense.
It can be assumed with a high degree of probability that this is a struggle between competitors (ticket distributors).
And also people who have already played the game so far, and have read enough, that they really think: how can this be - I count, count, and count again... and nag, there’s no way I can count.)
That is, they blame third-party forces for their failures, which do not allow them to calculate, well, no way.
Do you know where you can calculate something down to a fraction of a second? For example, in celestial mechanics - an eclipse of the moon - millennia in advance - based on past observations.
This, as we all know, was used by the priests who learned to predict such events.

In lotteries, alas, there are no regular intervals, for example, when a certain ball appears. Since we have randomness, and not clear celestial mechanics.
That is, if the chance of a number is 1 in 10, then it will play at random - somewhere, going into a deep pause, somewhere it will appear more often, BUT if we take a large number of tests, then on average the number will appear 10 times per draw.
The probability is leveled out.
I read calculations about jackpots.
Calculators took a fixed segment of the history of circulations - looked at how many jackpots they took - looked at how many bets they bought.
Simple division - and the result does not converge. That is, for example, in a 5 out of 36 lottery, the jackpot should be played out for every 376,992 bets)
It turned out, for example, 10 were played, but it should have been like 20)
They take another segment of the history of circulation, and repeat the calculation - and lo and behold, there is even more than calculated - which means it was fair there - and even the orgs gave more - like feeding.

Let's remember about a single number - draw on a time period (on a piece of paper) the history of the coincidence of a number, for example 33, over 150 draws.
Now divide this segment, say, into 3 equal parts. Count the number of matches in each part. You will find that there will be varying numbers of matches.
But on average for the entire segment, the probability will be close to the calculated one.
150 circulation is clearly not enough.

Now none of the calculators will agree to carry out calculations for, say, 3000 draws in 5 out of 36. This is titanic manual labor (you need to look at the number of purchased bets on the website and record the jackpots).
I am convinced that on average, for such a number of circulations, the probability will be about the calculated one.

18. Kazak 03.07.2017
I’m wondering how Stoloto differs from the casinos banned in the Russian Federation? Essentially the same bets on a number. Oh yes, just a different name))) Oh well, God bless the name. Here in the reviews they are hotly discussing the possibilities and chances of winning the lottery, they even made a combination generator. But where are these real people who win Jack Pots and big wins? I recommend watching several videos on YouTube about the organization of Stoloto lotteries, the random number generator (RNG), so-called live broadcasts, etc.

Answer:
People always want to win a lot of money for free. Any betting shop is built on this. To play or not, to believe or not, is everyone’s business. Link to video regarding Stoloto

19. lion 09.07.2017
I've been hooked on lotteries for about a year now. I understand with my mind that I have virtually no chance of winning the jackpot, but I just can’t tear myself away from the game.

20. Jobs 12.07.2017
Tell me how to correctly calculate the probability of one number falling out of a hundred

Answer:
The meaning of the question is not entirely clear. If we take a completely random, random drop, then the answer is quite obvious, the chances will be 1 in 100 for any number from 1 to 100.
If you are talking about random number generator (RNG) algorithms, then does any programming language have its own operator responsible for their generation? It is difficult to say how random it is, because a certain algorithm is still responsible for its operation, which in itself excludes complete randomness. But nevertheless, the end result is close to ideal.

21. Kiryusha 05.09.2017
Do not believe in the possibility of winning significant money in the lottery. All the money has been cut long ago. Search the Internet for information about the owner of Stoloto and how much money there is. In addition, all broadcasts are recorded. Any result can be returned. Dead souls get jackpots.

22. Nikolay 23.10.2017
What are you saying! Regarding the network, for example, you can find information on the Internet that the Earth is flat, and it turns out that everyone is deceived that it is a sphere... and you can find a lot more!
Have you ever seen the chances of winning? Can you imagine what this is all about? In lotteries, there is no need to “hustle”, since the probabilities will not allow the lottery to go bankrupt; the organizers will always make a profit.

And so that there are no doubts, or so that they are minimal, Russian state lotteries have been transferred to automatic lottery machines, to which no one approaches during the drawings. The lottery machines are installed behind glass in the lottery center. Now those interested can see with their own eyes the operation of these lottery machines - admission is free. By the way, there is no such openness anywhere else in the world.

news on the website stoloto.ru - the official website of Russian lotteries

23. lucky dude 26.10.2017
Nonsense, nonsense and more nonsense. Lady luck and nothing more. Try to take the combination given to you and beat it in the archive lottery and see what matches there were in past draws. Although who knows, maybe someone else will get the same bet taken from here. It's all up to chance

24. Andrey 27.10.2017
A good combination generator for stoloto STALKER LOTTO - 5x36, 6x45, 7x49, 6x49
The author on the program page provided links to the lottery forum where he conducted tests.

25. Semem Semenych 20.12.2017
>>>It is unlikely that you will find authors of lottery programs who will publicly conduct tests, and even on lottery forums, where the players are not at all stupid, who have gone through hundreds of free and paid programs.

I would say differently. It is unlikely that you will find avid lottery gamblers with high intelligence. Of course, they can buy 1-2-3 tickets for fun, but people understand perfectly well that it is simply unrealistic to win serious money in the lottery, especially in Russia.

26. Pavel 27.12.2017
Players with high intelligence do not play with several tickets - even for fun. Such players understand probability theory very well, which for most ordinary people is Chinese literacy. Such players play systematically, carefully calculating their chances and budget for the game. Such players develop strategies for the game. Such players never bet at random.

About winning in Russia big prizes- this is just your worldview, so to speak, not supported by any facts. Study better probability theory. It is extremely unlikely that your neighbor won the jackpot and then shared this information with you. I’ll say it differently - in Russia it’s dangerous to shine with a big win)))

27. I don’t play 05.01.2018
Pavel, people with high intelligence understand perfectly well what is a scam and what is not. And yes, their intelligence allows them much a larger share chances of making money than the lottery.

28. Alexander 16.01.2018
You can’t win at Stoloto, there’s a program for sold tickets

29. Mechanic 09.06.2018
Don’t fool your head, just take a screenshot of the lottery from the site and check after the drawing there is a winning, but they are cheap, I checked thousands, I’m tired of updating

30. match point 24.06.2018
I offer free and paid programs for lottery analysis: Keno, match point, 5/36, 6/45, 6/49, 7/49, Russian lotto and others. There is a built-in generator of combinations of given numbers, a win and jackpot generator, the ability to print lotto cards and much more. You can download it here [removed]

31. Ilya Nefedov 13.08.2018
Guys, no one will make you a state lotto winning generator 5 out of 36, etc. even taking into account past draws. Everything is clear about the chance of random numbers appearing. BUT! Only if they are truly random. And when winning combinations is generated by a computer that already knows what combinations the players have chosen, then I don’t believe in the honesty of its algorithms. The same as playing in an online casino, where the roulette generator already knows what bet you made.

32. Albert 08.11.2018
The program doesn't work at all, it forgets the numbers that are not needed. raw in one word

Answer:
I entered several different sets of exception numbers and ran them several dozen times in different modes. The indicated numbers never appeared in the result. Is it different for you? Or did I misunderstand you?

33. Albert 11.11.2018
How many numbers can be included in exceptions? I scored 30, there were replays from the elimination

Answer:
There are no restrictions. Do you separate numbers with a comma?
I add the following line to the exceptions:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30

Result: There are no excluded digits in the finished result.
If it’s different for you, please indicate your sequence and also your browser so that you can accurately recreate your situation.

34. Albert 14.11.2018
Opera browser. There are repetitions of those numbers that were typed in the exception
1.2.3.4.5.6.8.10.11.13.14.15.16.17.18.19.20.22.24.26.28.29.30.31.32.34.36.37.38.39.40.41.43.46.47.49.

Answer:
Your numbers are separated by a period and not a comma. It should be like this:
1,2,3,4,5,6,8,10,11,13,14,15,16,17,18,19,20,22,24,26,28,29,30,31,32,34,36,37,38,39,40,41,43,46,47,49
This combination works.

With this generator you can create 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 create one (1), two (2) or three digit (3) random numbers, but also five and seven. Easy to set up. Everyone can master it. You will also be able to select random numbers for online or offline lotteries or competitions. And it will be convenient. You can easily create entire tables or series of random numbers. In a split second you will receive a random number or a sequence of them (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 sweepstakes. By choosing, for example, the same range and number of numbers in the 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 completely free to use (no exceptions).

Enter range numbers

From
Before
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, division will be done by them, otherwise by spaces)


Note that ideally the random number distribution density curve would look as shown in Fig. 22.3. That is, in the ideal case, each interval includes same number points: N i = N/k , Where N — total number points, k number of intervals, i= 1, , k .

Rice. 22.3. Frequency diagram of random numbers,
generated theoretically by an ideal generator

It should be remembered that generating an arbitrary random number consists of two stages:

  • generating a normalized random number (that is, uniformly distributed from 0 to 1);
  • normalized random number conversion r i to random numbers x i, which are distributed according to the (arbitrary) distribution law required by the user or in the required interval.

Random number generators according to the method of obtaining numbers are divided into:

  • physical;
  • tabular;
  • algorithmic.

Physical RNG

An example of a physical RNG can be: a coin (“heads” 1, “tails” 0); dice; a drum with an arrow divided into sectors with numbers; hardware noise generator (HSG), which uses a noisy thermal device, for example, a transistor (Fig. 22.422.5).

Rice. 22.4. Scheme of a hardware method for generating random numbers
Rice. 22.5. Diagram of obtaining random numbers using the hardware method
Task “Generating random numbers using a coin”

Generate a random three-digit number, uniformly distributed in the range from 0 to 1, using a coin. Accuracy three decimal places.

The first way to solve the problem
Toss a coin 9 times, and if the coin lands on heads, then write down “0”; if it lands on heads, then write down “1”. So, let’s say that as a result of the experiment we received the random sequence 100110100.

Draw an interval from 0 to 1. Reading the numbers in sequence from left to right, split the interval in half and each time choose one of the parts of the next interval (if a 0 comes up, then the left one, if a 1 comes up, then the right one). Thus, you can get to any point in the interval, as accurately as you like.

So, 1 : the interval is divided in half and , the right half is selected, the interval is narrowed: . Next number 0 : the interval is divided in half and , the left half is selected, the interval is narrowed: . Next number 0 : the interval is divided in half and , the left half is selected, the interval is narrowed: . Next number 1 : the interval is divided in half and , the right half is selected, the interval is narrowed: .

According to the accuracy condition of the problem, a solution has been found: it is any number from the interval, for example, 0.625.

In principle, if we take a strict approach, then the division of intervals must be continued until the left and right boundaries of the found interval COINCIDE with an accuracy of the third decimal place. That is, from the point of view of accuracy, the generated number will no longer be distinguishable from any number from the interval in which it is located.

The second way to solve the problem
Let's split the resulting binary sequence 100110100 into triads: 100, 110, 100. After translating these binary numbers in decimal we get: 4, 6, 4. Substituting “0.” in front, we get: 0.464. This method can only produce numbers from 0.000 to 0.777 (since the maximum that can be “squeezed out” from three binary digits is 111 2 = 7 8) that is, in fact, these numbers are represented in octal system Reckoning For translate octal numbers in decimal let's execute the representation:
0.464 8 = 4 8 1 + 6 8 2 + 4 8 3 = 0.6015625 10 = 0.602 10.
So, the required number is: 0.602.

Tabular RNG

Tabular RNGs use specially compiled tables containing verified uncorrelated, that is, in no way dependent on each other, numbers as a source of random numbers. In table Figure 22.1 shows a small fragment of such a table. By traversing the table from left to right from top to bottom, you can obtain random numbers evenly distributed from 0 to 1 with the required number of decimal places (in our example, we use three decimal places for each number). Since the numbers in the table do not depend on each other, the table can be traversed different ways, for example, from top to bottom, or from right to left, or, say, you can select numbers that are in even positions.

Table 22.1.
Random numbers. Evenly
random numbers distributed from 0 to 1
Random numbers Evenly distributed
0 to 1 random numbers
9 2 9 2 0 4 2 6 0.929
9 5 7 3 4 9 0 3 0.204
5 9 1 6 6 5 7 6 0.269
… …

The advantage of this method is that it produces truly random numbers, since the table contains verified uncorrelated numbers. Disadvantages of the method: for storage large quantity numbers require a lot of memory; There are great difficulties in generating and checking such tables; repetitions when using a table no longer guarantee randomness number sequence, and therefore the reliability of the result.

There is a table containing 500 absolutely random verified numbers (taken from the book by I. G. Venetsky, V. I. Venetskaya “Basic mathematical and statistical concepts and formulas in economic analysis”).

Algorithmic RNG

The numbers generated by these RNGs are always pseudo-random (or quasi-random), that is, each subsequent number generated depends on the previous one:

r i + 1 = f(r i) .

Sequences made up of such numbers form loops, that is, there is necessarily a cycle that repeats an infinite number of times. Repeating cycles are called periods.

The advantage of these RNGs is their speed; generators require virtually no memory resources and are compact. Disadvantages: the numbers cannot be fully called random, since there is a dependence between them, as well as the presence of periods in the sequence of quasi-random numbers.

Let's consider several algorithmic methods for obtaining RNG:

  • method of median squares;
  • method of middle products;
  • stirring method;
  • linear congruent method.

Midsquare method

There is some four-digit number R 0 . This number is squared and entered into R 1 . Next from R 1 takes the middle (four middle digits) new random number and writes it into R 0 . Then the procedure is repeated (see Fig. 22.6). Note that in fact, as a random number you need to take not ghij, A 0.ghij with a zero and a decimal point written on the left. This fact is reflected as in Fig. 22.6, and in subsequent similar figures.

Rice. 22.6. Scheme of the mean squares method

Disadvantages of the method: 1) if at some iteration the number R 0 will become equal to zero, then the generator degenerates, so the correct choice of the initial value is important R 0 ; 2) the generator will repeat the sequence through M n steps (in best case scenario), Where n number digit R 0 , M base of the number system.

For example in Fig. 22.6: if the number R 0 will be presented in binary system number, then the sequence of pseudo-random numbers will be repeated in 2 4 = 16 steps. Note that the repetition of the sequence can occur earlier if the starting number is chosen poorly.

The method described above was proposed by John von Neumann and dates back to 1946. Since this method turned out to be unreliable, it was quickly abandoned.

Midproduct method

Number R 0 multiplied by R 1, from the result obtained R 2 the middle is extracted R 2 * (this is another random number) and multiplied by R 1 . All subsequent random numbers are calculated using this scheme (see Fig. 22.7).

Rice. 22.7. Scheme of the method of median products

Stirring method

The shuffle method uses operations to cyclically shift the contents of a cell left and right. The idea of ​​the method is as follows. Let the cell store the initial number R 0 . Cyclically shifting the cell contents to the left by 1/4 of the cell length, we get a new number R 0 * . In the same way, cycling the contents of the cell R 0 to the right by 1/4 of the cell length, we get the second number R 0**. Sum of numbers R 0* and R 0** gives a new random number R 1 . Further R 1 is entered in R 0, and the entire sequence of operations is repeated (see Fig. 22.8).


Rice. 22.8. Mixing method diagram

Please note that the number resulting from the summation R 0* and R 0 ** , may not fit completely in the cell R 1 . In this case, the extra digits must be discarded from the resulting number. Let us explain this in Fig. 22.8, where all cells are represented by eight binary digits. Let R 0 * = 10010001 2 = 145 10 , R 0 ** = 10100001 2 = 161 10 , Then R 0 * + R 0 ** = 100110010 2 = 306 10 . As you can see, the number 306 occupies 9 digits (in the binary number system), and the cell R 1 (same as R 0) can contain a maximum of 8 bits. Therefore, before entering the value into R 1, it is necessary to remove one “extra”, leftmost bit from the number 306, resulting in R 1 will no longer go to 306, but to 00110010 2 = 50 10 . Also note that in languages ​​such as Pascal, “trimming” of extra bits when a cell overflows is performed automatically in accordance with the specified type of the variable.

Linear congruent method

The linear congruent method is one of the simplest and most commonly used procedures currently simulating random numbers. This method uses the mod( x, y) , which returns the remainder when the first argument is divided by the second. Each subsequent random number is calculated based on the previous random number using the following formula:

r i+ 1 = mod( k · r i + b, M) .

The sequence of random numbers obtained using this formula is called linear congruent sequence. Many authors call a linear congruent sequence when b = 0 multiplicative congruent method, and when b ≠ 0 — mixed congruent method.

For a high-quality generator, it is necessary to select suitable coefficients. It is necessary that the number M was quite large, since the period cannot have more M elements. On the other hand, the division used in this method is a rather slow operation, so for a binary computer the logical choice would be M = 2 N, since in this case finding the remainder of division is reduced inside the computer to binary logical operation"AND". Choosing the largest prime number is also common M, less than 2 N: in the specialized literature it is proven that in this case the low-order digits of the resulting random number r i+ 1 behave just as randomly as the older ones, which has a positive effect on the entire sequence of random numbers as a whole. As an example, one of the Mersenne numbers, equal to 2 31 1, and thus, M= 2 31 1 .

One of the requirements for linear congruent sequences is that the period length be as long as possible. The length of the period depends on the values M , k And b. The theorem we present below allows us to determine whether it is possible to achieve a period of maximum length for specific values M , k And b .

Theorem. Linear congruent sequence defined by numbers M , k , b And r 0, has a period of length M if and only if:

  • numbers b And M relatively simple;
  • k 1 times p for every prime p, which is a divisor M ;
  • k 1 is a multiple of 4, if M multiple of 4.

Finally, let's conclude with a couple of examples of using the linear congruent method to generate random numbers.

It was determined that a series of pseudo-random numbers generated based on the data from example 1 would be repeated every M/4 numbers. Number q is set arbitrarily before the start of calculations, however, it should be borne in mind that the series gives the impression of being random at large k(and therefore q). The result can be improved somewhat if b odd and k= 1 + 4 · q in this case the row will be repeated every M numbers. After a long search k the researchers settled on values ​​of 69069 and 71365.

A random number generator using the data from Example 2 will produce random, non-repeating numbers with a period of 7 million.

The multiplicative method for generating pseudorandom numbers was proposed by D. H. Lehmer in 1949.

Checking the quality of the generator

The quality of the entire system and the accuracy of the results depend on the quality of the RNG. Therefore, the random sequence generated by the RNG must satisfy a number of criteria.

The checks carried out are of two types:

  • checks for uniformity of distribution;
  • tests for statistical independence.

Checks for uniformity of distribution

1) The RNG should produce close to following values statistical parameters characteristic of a uniform random law:

2) Frequency test

A frequency test allows you to find out how many numbers fall within an interval (m r – σ r ; m r + σ r) , that is (0.5 0.2887; 0.5 + 0.2887) or, ultimately, (0.2113; 0.7887). Since 0.7887 0.2113 = 0.5774, we conclude that in a good RNG, about 57.7% of all random numbers drawn should fall into this interval (see Fig. 22.9).

Rice. 22.9. Frequency diagram of an ideal RNG
in case of checking it for frequency test

It is also necessary to take into account that the number of numbers falling into the interval (0; 0.5) should be approximately equal to the number of numbers falling into the interval (0.5; 1).

3) Chi-square test

The chi-square test (χ 2 test) is one of the most well-known statistical tests; it is the main method used in combination with other criteria. The chi-square test was proposed in 1900 by Karl Pearson. His remarkable work is considered as the foundation of modern mathematical statistics.

For our case, testing using the chi-square criterion will allow us to find out how much the real The RNG is close to the RNG benchmark, that is, whether it satisfies the uniform distribution requirement or not.

Frequency diagram reference The RNG is shown in Fig. 22.10. Since the distribution law of the reference RNG is uniform, then the (theoretical) probability p i getting numbers into i th interval (all these intervals k) is equal to p i = 1/k . And thus, in each of k intervals will hit smooth By p i · N numbers ( N — total generated numbers).

Rice. 22.10. Frequency diagram of the reference RNG

A real RNG will produce numbers distributed (and not necessarily evenly!) across k intervals and each interval will contain n i numbers (in total n 1 + n 2 + + n k = N ). How can we determine how good the RNG being tested is and how close it is to the reference one? It is quite logical to consider the squared differences between the resulting number of numbers n i and "reference" p i · N . Let's add them up and the result is:

χ 2 exp. = ( n 1 p 1 · N) 2 + (n 2 p 2 · N) 2 + + ( n k – p k · N) 2 .

From this formula it follows that the smaller the difference in each of the terms (and therefore the less valueχ 2 exp. ), the stronger the law of distribution of random numbers generated by a real RNG tends to be uniform.

In the previous expression, each of the terms is assigned the same weight (equal to 1), which in fact may not be true; therefore, for chi-square statistics, it is necessary to normalize each i th term, dividing it by p i · N :

Finally, let’s write the resulting expression more compactly and simplify it:

We obtained the chi-square test value for experimental data.

In table 22.2 are given theoretical chi-square values ​​(χ 2 theoretical), where ν = N 1 is the number of degrees of freedom, p this confidence probability, specified by the user, which indicates how much the RNG should satisfy the requirements of uniform distribution, or p — is the probability that the experimental value of χ 2 exp..

will be less than the tabulated (theoretical) χ 2 theoretical.
or equal to it
Table 22.2. Some percentage points of the χ 2 distribution p = 1% p = 5% p = 25% p = 50% p = 75%
ν = 1 0.00016 0.00393 0.1015 0.4549 1.323 3.841 6.635
ν = 2 0.02010 0.1026 0.5754 1.386 2.773 5.991 9.210
ν = 3 0.1148 0.3518 1.213 2.366 4.108 7.815 11.34
ν = 4 0.2971 0.7107 1.923 3.357 5.385 9.488 13.28
ν = 5 0.5543 1.1455 2.675 4.351 6.626 11.07 15.09
ν = 6 0.8721 1.635 3.455 5.348 7.841 12.59 16.81
ν = 7 1.239 2.167 4.255 6.346 9.037 14.07 18.48
ν = 8 1.646 2.733 5.071 7.344 10.22 15.51 20.09
ν = 9 2.088 3.325 5.899 8.343 11.39 16.92 21.67
ν = 10 2.558 3.940 6.737 9.342 12.55 18.31 23.21
ν = 11 3.053 4.575 7.584 10.34 13.70 19.68 24.72
ν = 12 3.571 5.226 8.438 11.34 14.85 21.03 26.22
ν = 15 5.229 7.261 11.04 14.34 18.25 25.00 30.58
ν = 20 8.260 10.85 15.45 19.34 23.83 31.41 37.57
ν = 30 14.95 18.49 24.48 29.34 34.80 43.77 50.89
ν = 50 29.71 34.76 42.94 49.33 56.33 67.50 76.15
ν > 30 ν p = 95% ν ) · x p p = 99% x 2 p+ sqrt(2 + 2/3 · 2/3 + ν ))
x p = O (1/sqrt( 2.33 0.00 0.674 1.64 2.33

1.64 p 0.674.

Considered acceptable p from 10% to 90% If χ 2 exp. much more than χ 2 theory. n i(that is p i · N is large), then the generator

Even D. Knuth in his book “The Art of Programming” noted that having χ 2 exp.

In general, it’s not good for small ones either, although at first glance this seems wonderful from the point of view of uniformity. Indeed, take a series of numbers 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, they are ideal from the point of view of uniformity, and χ 2 exp. p will be practically zero, but you are unlikely to recognize them as random. If χ 2 exp. If χ 2 exp. n i much less than χ 2 theory. p i · N (that is

small), then the generator p the requirement of a random uniform distribution, since the observed values p too close to theoretical

and cannot be considered random. p i · N But if χ 2 exp.

lies in a certain range between two values ​​of χ 2 theor. , which correspond, for example,

= 25% and

= 50%, then we can assume that the random number values ​​generated by the sensor are completely random.

In addition, it should be borne in mind that all values

must be large enough, for example more than 5 (found empirically). Only then (with a sufficiently large statistical sample) can the experimental conditions be considered satisfactory. p i So, the verification procedure is as follows. i Tests for statistical independence

1) Checking for the frequency of occurrence of numbers in the sequence

Let's look at an example. The random number 0.2463389991 consists of the digits 2463389991, and the number 0.5467766618 consists of the digits 5467766618. Connecting the sequences of digits, we have: 24633899915467766618. n It is clear that the theoretical probability loss It is clear that the theoretical probability The th digit (from 0 to 9) is equal to 0.1. It is clear that the theoretical probability 2) Checking the appearance of series of identical numbers m Let us denote by m L

number of series of identical digits in a row of length n. Everything needs to be checked n 3 = 2 .

from 1 to It is clear that the theoretical probability, Where p It is clear that the theoretical probability this is a user-specified number: the maximum occurring number of identical digits in a series. It is clear that the theoretical probability In the example “24633899915467766618” 2 series of length 2 (33 and 77) were found, that is p 2 = 2 and 2 series of length 3 (999 and 666), that is p The probability of occurrence of a series of length p is equal to:

= 9 10 p It is clear that the theoretical probability= 0.9, since there can be only one symbol out of 10, and there are 9 symbols in total (zero does not count). And the probability that two identical symbols “XX” will appear in a row is 0.1 · 0.1 · 9, that is, the probability of 0.1 that the symbol “X” will appear in the first position is multiplied by the probability of 0.1 that the same symbol will appear in the second position “X” and multiplied by the number of such combinations 9.

The frequency of occurrence of series is calculated using the chi-square formula we previously discussed using the values p It is clear that the theoretical probability .

Note: The generator can be tested multiple times, but the tests are not complete and do not guarantee that the generator produces random numbers. For example, a generator that produces the sequence 12345678912345 will be considered ideal during tests, which is obviously not entirely true.

In conclusion, we note that the third chapter of Donald E. Knuth's book The Art of Programming (Volume 2) is entirely devoted to the study of random numbers. It examines various methods for generating random numbers, statistical criteria for randomness, and the conversion of uniformly distributed random numbers to other types. random variables. More than two hundred pages are devoted to the presentation of this material.