﻿ Random Numbers - Painful C Language
C language learned by suffering

### random number

##### pseudorandom number
Random numbers are, as the name implies, random numbers.
In essence, think of it as the same as dice.

Random numbers are essential in games that require random numbers.
Also, when analyzing complex phenomena or statistical properties, such as
Random numbers allow for easy experimentation.

But as you all know, computers are very precise machines.
It is not possible to create numbers that are essentially random.
Therefore, a technique called pseudorandom numbers is used to obtain random numbers through computation.

Keywords.
##### Pseudorandom number

A method of obtaining random numbers by calculation.
Not truly random, but can be considered random for all practical purposes.

In pseudo-random numbers, the number that appears to be random is created solely by calculation.
However, since you can actually get quite disparate numbers
It is safe to assume that the number is almost random.

Calculation of pseudorandom numbers
There are many different ways to compute pseudorandom numbers.
Most of the methods provided by the C language are linear congruent methods.

I won't bore you with the details, but a simple explanation is that
X = appropriate number * X (truncate upper digits to prevent increase)
The calculation is to obtain a different value each time by repeatedly doing the same thing.

This method is simple, but not very random.
When used in combination several times, it will result in the same pattern.
However, for games and other applications, the values are random enough.

##### Create a random number
Now that we have a rough idea of what a pseudorandom number is.
I would like to try using pseudorandom numbers right away.
The C language provides a rand function to create pseudo-random numbers.
Note that you must #include <stdlib.h> to use the rand function.

rand function
`` Variable = rand();``

There is no need to pass any parameters to the rand function.
Simply use it as is to compute random values.
The following program is an example of having a random number calculated 10 times.

Source Code
`````` #include <stdio.h>
#include <stdlib.h>

int main(void)
{
int i;

for (i = 0; i < 10; i++) {
printf("%d\n", rand());
}

return 0;
}``````

The results of running this program might be as follows

Execution Result
130
10982
1090
11656
7117
17595
6415
22948
31126
9004

As you can see, random values are obtained.
##### Limit the range of random numbers
In the previous section, we saw how to calculate random numbers.
This is difficult to use because the numbers are too disparate.
Is it possible to get a certain range of random numbers, like 1-6 on a dice?

If we know the maximum value we can get, then we can divide it equally.
In C, the maximum value obtained by the rand function can be found by the value of the constant RAND_MAX.

Therefore, we can divide the value obtained by the rand function by the value that divides RAND_MAX equally.
It is quite tedious to calculate the formula for this purpose, so I will introduce the formula.

Range Random Number Formula
Minimum + (int)( rand() * (maximum - minimum + 1.0) / (1.0 + RAND_MAX) )

You do not have to know what this formula means.
Anyway, if you follow this procedure, you can calculate random numbers in the range of minimum to maximum values.
The following program creates a GetRandom function that calculates the above formula, and then creates a
Here is an example of rolling the dice 10 times using the GetRandom function.

Source Code
`````` #include <stdio.h>
#include <stdlib.h>

int GetRandom(int min, int max);

int main(void)
{
int i;

for (i = 0; i < 10; i++) {
printf("%d\n", GetRandom(1, 6));
}

return 0;
}

int GetRandom(int min, int max)
{
return min + (int)(rand() * (max - min + 1.0) / (1.0 + RAND_MAX));
}``````

The results of running this program might be as follows

Execution Result
1
3
1
3
2
4
2
5
6
2

As you can see, a random number ranging from 1 to 6 is obtained.

Mathematics and Computers
It is generally believed that mathematics is important for using computers.
While this is essentially true, there are some aspects that are not so true.
Because if you use a computer, it does the math automatically.
This is because as long as you know the formula, you can simply apply it.

##### Different random numbers each time.
Using the GetRandom function created in the previous section, you can calculate any random number you like.
In fact, however, there is still a problem that remains to be considered.

As explained in the beginning, pseudo-random numbers are computationally based.
In other words, if they were created based on the same number, they would be the same random number.

To confirm this, we will run the program created in the previous section twice.

Execution result 1st
1
3
1
3
2
4
2
5
6
2

Execution result 2nd
1
3
1
3
2
4
2
5
6
2

To my surprise, I got exactly the same value the first and second time. It is the same no matter how many times you do it.
This is not a very good substitute for dice.

To solve this problem, we need to change the original number used to calculate random numbers.
The srand function is provided for this purpose.

srand function
`` srand(original number);``

However, even if you use the srand function to put in a different number
If the original number is the same when it is executed, it will be the same random number, so it is not a solution.
Of course, putting a rand function in the srand function is meaningless because the random numbers created at the beginning are the same.
There is a way to have the user enter the information, but it is a labor-intensive and unfriendly method.

In short, I want to somehow get a completely bullshit number into the srand function, but
There is one method that is perfect for this. It is to insert the current time.
By putting the current time in seconds into the srand function, the original number, which is different each time, can be used as a random number.

The function to get the current time must #include <time.h> in the time function.
The srand and time functions can be used to compute a different random number each time as follows

Calculate a different random number each time
`` srand((unsigned int)time(NULL));``

In the case of random number generation, it is not necessary to know how to use the time function.
Note that it is cast to the type unsigned int, which is an unsigned integer value.
It is sufficient to do this process once when starting the program.
The following program is an example of calculating a different random number each time with the addition of the above process.

Calculate a different random number each time
`````` #include <stdio.h>
#include <stdlib.h>
#include <time.h>

int GetRandom(int min, int max);

int main(void)
{
int i;

srand((unsigned int)time(NULL));

for (i = 0; i < 10; i++) {
printf("%d\n", GetRandom(1, 6));
}
return 0;
}

int GetRandom(int min, int max)
{
return min + (int)(rand() * (max - min + 1.0) / (1.0 + RAND_MAX));
}``````

The result of two runs of this program might be as follows ````

Execution result 1st
6
6
5
4
6
5
1
3
2
6

Execution result 2nd
5
2
2
4
3
5
4
1
3
1

You can see that we get spectacularly different values. Note that here we are using the srand function in the main function, but the
This can be included in the GetRandom function by doing the following

Calculate a different random number each time
`````` int GetRandom(int min, int max)
{
static int flag;

if (flag == 0) {
srand((unsigned int)time(NULL));
flag = 1;
}

return min + (int)(rand() * (max - min + 1.0) / (1.0 + RAND_MAX));
}``````

By using static variables, the srand function is used only once at the beginning.

Comment
COMMENT

Open the 💬 comment submission box