"A random number is, as the name suggests, a number chosen randomly."
Basically, think of it as like a die.
Random number generation is essential in games that require random numbers.
Furthermore, when analyzing complex phenomena or statistical properties,
You can easily conduct experiments by using random numbers.
However, as you all know, computers are very precise machines.
Essentially, you cannot truly generate random numbers.
Therefore, a technique called pseudo-random numbers, which are generated by calculation, is used.
"With pseudo-random numbers, we're essentially generating numbers that appear random through calculation."
However, you can actually obtain quite varied results.
I think it's fair to consider them to be almost random numbers.
Pseudo-random number generation
Pseudorandom numberにはvarious calculations方法があるのですが、 C languageで用意されるのはほとんどが線形合同法is.
詳しいDescriptionは省きますが、簡単にDescriptionすれば、 X = 適当な数 * X(上位ケタの部minuteを切り捨てて増加を防ぐ) をひたすら繰り返すことで毎回異なる値を得る計算is.
Now that you have a general idea of what pseudorandom numbers are,
I'd like to try using a pseudorandom number generator now.
C has a function called rand to generate pseudo-random numbers.
"Note that you need to include <stdlib.h> to use the rand function."
rand function
variable = rand();
The rand function doesn't require any parameters.
Calculates random values with no setup required.
The following program demonstrates an example of calculating random numbers 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;
}
"In the previous section, I understood how to calculate random numbers,"
This data is too scattered to be useful.
Isn't it possible to obtain a random number within a certain range, like getting 1 to 6 with a die?
If you know the maximum possible value, you can divide it equally.
In C, the maximum value obtainable from the `rand` function is determined by the value of the constant `RAND_MAX`.
Therefore, you need to divide the value obtained from the `rand` function by a value that equally divides RAND_MAX.
Calculating the formula for that is quite tedious, so I'll introduce a formula.
You don't need to understand the meaning of this formula.
Anyway, if you do it this way, you can calculate a random number within the minimum-to-maximum range.
The following program creates a GetRandom function to calculate the formula above.
Here's an example of rolling a die 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 output of this program may be as follows.
Execution results
1 3 1 3 2 4 2 5 6 2
As you can see, a random number between 1 and 6 has been generated.
You can calculate any random number using the GetRandom function created in the previous section.
However, in reality, there are still issues that need to be considered.
As previously explained, pseudo-random numbers are computer-generated.
Essentially, if generated from the same initial number, you will get the same random number.
To verify this, let's run the program created in the preceding section twice.
First execution result
1 3 1 3 2 4 2 5 6 2
Second execution result
1 3 1 3 2 4 2 5 6 2
Surprisingly, the values obtained in the first and second trials are exactly the same.No matter how many times I do it, it's the same.
It's really not a substitute for a die.
To solve this problem, we need to change the original numbers used for calculating random numbers.
For that purpose, the srand function is provided.
srand function
srand(元の数);
However, even if you use the srand function with a different number,
"It won't solve the problem because the random number will be the same if the original number is the same when it's executed."
"Of course, putting the `rand` function into the `srand` function is pointless because the initial random numbers will be the same."
There's also a way to let the user input it, but it's an inconvenient and time-consuming method.
Basically, I really need to somehow force `srand` to take in completely random numbers.
There's a perfect way to do it.That's how to insert the current time.
If you feed the current time in seconds into the `srand` function, you can use different seed values for random numbers each time.
The function to obtain the current time is time() and requires you to #include <time.h>.
"You can calculate different random numbers each time by using the srand() function and the time() function in this way."
Calculate a different random number each time.
srand((unsigned int)time(NULL));
You don't need to know how to use the time function if you're using a random number generator, just follow these instructions.
"Note that it is cast to an unsigned int type, which represents an unsigned integer value."
This process only needs to be performed once when the program starts.
The following program is an example of how to incorporate the above process to calculate a different random number each time.
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 results of running this program twice may be as follows. ```
First execution result
6 6 5 4 6 5 1 3 2 6
Second execution result
5 2 2 4 3 5 4 1 3 1
You can see that distinct values are obtained. Meanwhile, the srand function is being used in the main function here.
Here's how we can incorporate this into the GetRandom function.
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 a static variable, we ensure that the srand function is called only once initially.
About This Site
Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.