Number generator is a convenient tool that allows you to get the required number of numbers of a given digit and the widest range. Our Random Number Generator has many applications! For example, you can hold a contest or draw a teddy bear in a group of bikers for repost:)). We will also be very flattered if you decide to use it to determine the winning number in a lottery or decide on what number to bet in the casino. We really hope that someone will find their lucky number online with us!
The ability to generate random numbers is very useful in some types of programs, in particular, in games, scientific or statistical simulation programs. Take, for example, games without random (or “random”) events – monsters will always attack you in the same way, you will always find the same objects / artifacts, dungeon and dungeon models will never change, etc. In general, the plot of such a game is not very interesting and it is unlikely that you will play it for a long time.
Story of origin
A simple dice, widely used in gambling in the past and in board games today, is the simplest true random number generator. In 1890, the English researcher Francis Galton described a way to use the game dice to generate random numbers for scientific purposes.
Further development of hardware random number generators can be considered special devices – lototrons, used to generate numbers in the lotto and keno. They mainly consist of a drum that mixes the balls with numbers, and a device that extracts them from it one by one. However, this generation method is very slow and unsuitable for the automatic generation of large arrays of data.
Large arrays were needed for the application tasks. In 1939, Kendall and Babington-Smith built the first machine that generated random numbers to build a table containing 100,000 random numbers. And 16 years later RAND Corporation, using special devices, built a table of a million random numbers. Despite the revival of the table method in 1996 by Marsalleu, who built 650 Mbytes of random numbers, the range of applicability of such tables is very narrow.
Random number generators, which generate them in real time, have become much more widespread. In 1951, the computer Ferranti Mark
a program was turned on which generated random numbers using the noise of the resistor. The idea of creating this program was A. Turing. And in 1957 the ERNIE (Electronic Random Number Indicator Equipment) machine was invented, the fourth version of which was presented in 2004. This device was originally designed to generate numbers of winning bonds in the British lottery.
Numbers surround us from birth and play an important role in our lives. In many people, the work itself is related to numbers, someone relies on luck to fill in the numbers in the lottery tickets, and someone gives them even mystical meaning. One way or another, sometimes we cannot do without using a program such as random number generator.
Pseudo-random number generator
So how do you generate random numbers? In real life we often flip a coin (eagle/nut), bone or shuffle cards. These events include so many physical variables (e.g. gravity, friction, air resistance, etc.) that they become almost impossible to predict/control and produce results that in all senses are random.
However, computers are not designed to use physical variables – they cannot flip a coin, roll a dice or shuffle real maps. Computers live in a controlled electrical world where there are only two values (true and false) and there is nothing average between them. By nature, computers are designed to produce predictable results. When we tell a computer to count 2 + 2, we always want the answer to be 4 (not 3 and not 5).
Consequently, computers are unable to generate random numbers. Instead they can simulate a random number, which is achieved with pseudo-random number generators.
A pseudo-random number generator is a program that takes the starting/starting value and performs certain mathematical operations with it in order to convert it to another number that is not related to the starting value at all. The program then uses the newly generated value and performs the same mathematical operations with it as with the initial number to convert it into another new number, the third one, which has no relation to either the first or the second one. By applying this algorithm to the last generated value, the program can generate a number of new numbers that seem random (provided that the algorithm is sufficiently complex).
In fact, it is not that difficult to write a simple algorithm. Here is a small program that Random numbers generator.
The result of the program execution:
18256 4675 32406 6217 27484
975 28066 13525 25960 2907
12974 26465 13684 10471 19898
12269 23424 23667 16070 3705
22412 9727 1490 773 10648
1419 8926 3473 20900 31511
5610 11805 20400 1699 24310
25769 9148 10287 32258 12597
19912 24507 29454 5057 19924
11591 15898 3149 9184 4307
24358 6873 20460 2655 22066
16229 20984 6635 9022 31217
10756 16247 17994 19069 22544
31491 16214 12553 23580 19599
3682 11669 13864 13339 13166
16417 26164 12711 11898 26797
27712 17715 32646 10041 18508
28351 9874 31685 31320 11851
9118 26193 612 983 30378
26333 24688 28515 8118 32105
Each number seems random in relation to the previous one. The main disadvantage of this algorithm is its primitiveness.
Functions srand() and rand()
The C and C++ languages have their own built-in random number generators. They are implemented in 2 separate functions, which are located in the cstdlib header file:
The srand() function sets the value passed by the user as a start value. srand() should be called only once – at the beginning of the program (usually at the top of the main() function).
The rand() function generates the next random number in the sequence. It will be in the range from 0 to RAND_MAX (a constant in cstdlib whose value is 32767).
Here is an example of a program that uses both these functions:
14867 24680 8872 25432 21865
17285 18997 10570 16397 30572
22339 31508 1553 124 779
6687 23563 5754 25989 16527
19808 10702 13777 28696 8131
18671 27093 8979 4088 31260
31016 5073 19422 23885 18222
3631 19884 10857 30853 32618
31867 24505 14240 14389 13829
13469 11442 5385 9644 9341
11470 189 3262 9731 25676
1366 24567 25223 110 24352
24135 459 7236 17918 1238
24041 29900 24830 1094 13193
10334 6192 6968 8791 1351
14521 31249 4533 11189 7971
5118 19884 1747 23543 309
28713 24884 1678 22142 27238
6261 12836 5618 17062 13342
14638 7427 23077 25546 21229
Start number and sequences in Random Number Generator
If you run the above program (random number generation) several times, you will notice that the results always contain the same numbers! This means that although each number in the sequence seems random from the previous one, the whole sequence is not random at all! This, in turn, means that our program is completely predictable (the same input values lead to the same output values). There are times when this may be useful or even desirable (e.g. if you want the scientific simulation to repeat itself, or if you’re trying to fix the reasons for your random dungeon generator failure in the game).
But in most cases this is not exactly what we need. If you write a game like Hi-Lo (where the user has 10 attempts to guess the number and the computer tells him how close his guess is or how far from the real number), you would not want the program to choose the same numbers each time. So let’s take a closer look at why this happens and how it can be corrected.
Remember that each new number in the Random number generation sequence is generated from the previous one in a certain way. Thus, with a constant initial number Random number generation will always generate the same sequence! In the program above, the sequence of numbers is always the same, because the starting number is always 4541.
To fix this, we need a way to select a starting number that is not a fixed value. The first thing that comes to mind is to use a random number! This is a good idea, but if we need a random number to generate random numbers, it is some kind of closed circle, don’t you think? It turns out that we don’t need to use a random starting number – we just need to choose something that will change every time the program starts up again. Then, we can use our Random number generation to generate a unique sequence of random numbers based on a unique starting number.
A common solution is to use a system clock. Each time the program starts up, the time will be different. If we use the time value as a starting number, our program will always generate a different sequence of numbers every time we start up the program!
The C language has a function time() which returns as time the total number of seconds elapsed since midnight on January 1st 1970. To use this function, we just need to connect the ctime header file and then initialize the srand() function by calling the time(0) function.
Random number generation in specified range
In most cases we do not need random numbers between 0 and RAND_MAX – we need numbers between two other values: min and max. For example, if we need to simulate a die roll, the range of values is small: from 1 to 6.
Here is a small function that converts the rand() function result into the range of values we need:
// Generate a random number between min and max values.
// It is assumed that the srand() function has already been called
int getRandomNumber(int min, int max)
{
static const double fraction = 1.0 / (static_cast(RAND_MAX) + 1.0);
// Evenly distribute a random number in our range
return static_cast(rand() * fraction * (max - min + 1) + min);
}
1
2
3
4
5
6
7
8
// Generate a random number between min and max values.
// It is assumed that the srand() function has already been called
int getRandomNumber(int min, int max)
{
static const double fraction = 1.0 / (static_cast(RAND_MAX) + 1.0);
// Evenly distribute a random number in our range
return static_cast(rand() * fraction * (max - min + 1) + min);
}
To simulate a die roll, call the getRandomNumber(1) function,