- ..clause 1..: Quasi-random number
- ..clause 2..: Make random numbers.
- ..clause 3..: Limit the range of random numbers.
- ..clause 4..: Make it to an every time different random numbers.
Random numbers are random as its name suggests numbers.
Random numbers do not lack it in the game to need a random number.
Moreover, when you analyze a complex phenomenon and a statistical character
It is possible to experiment easily by using random numbers.
However, the computer is a very accurate machine in you as you know.
The number cannot be made essentially at random.
Then, the technique of quasi-random number to obtain a random number by the calculation is used.
In the quasi-random number, the number that the calculation looks random to the last is made.
[ Quasi-random number ]
Method of obtaining random numerical value by calculation.
You may think it is realistically random though it is true and not random.
[ Calculation of quasi-random number ]
Though there are various computational methods in the quasi-random number
In the preparation by C language, most is a linear congruential method.
A detailed explanation : though omits if it explains easily.
X number *= suitable X(Round down the part of a high-ranking digit and prevent an increase. )It is a calculation that obtains an every time different because of single-mindedly repeat [wo] value.
It doesn't become it at random too much though this method is simple.
It becomes the same pattern if it combines several times and it uses it.
However, if it is a usage of the game etc. , it becomes a very random value.
In the place where it was roughly understood what the quasi-random number is
I want to use the quasi-random number immediately.
The rand function that makes the quasi-random number is prepared in C language.
To use the rand function # include should do < stdlib.h >.
The parameter etc. need not be especially passed to the rand function.変数 = rand();
The execution result of this program might become as follows.#include <stdio.h> #include <stdlib.h> int main(void) { int i; for (i = 0;i < 10;i++) { printf("%d\n",rand()); } return 0; }
As you see, a random value is obtained.
130
10982
1090
11656
7117
17595
6415
22948
31126
9004
Though the method of calculating random numbers in the preceding clause was understood
Because the number is too various, this is not used easily.
Cannot random numbers within a certain range be obtained as 1-6 of the dice?
Will only have to divide it equally if you understand the maximum value of the obtained value.
In C language, the maximum value obtained by the rand function : It understands from the value of the constant named RAND_MAX.
Therefore, though the value obtained by the rand function only has to be divided by the value in which RAND_MAX is divided equally
Calculating the expression for that introduces the formula because it is considerably troublesome.
[ Range random numbers official ]Do not understand and do not care about this official meaning.
Minimum value + (int)( rand() * (maximum value-minimum value + 1.0) / (1.0 + RAND_MAX))
The execution result of this program might become as follows.#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)); }
As you see, a random number within the range of 1-6 is obtained.
1
3
1
3
2
4
2
5
6
2
[ Mathematics and computer ]
In general, it is thought that mathematics is important to be computer-aided.
There is a part that cannot be said that it is so though it is the essentially street.
The calculation : because it does automatically if computer-aided.
It is because of ending only where it is applied as long as it knows the formula.
If the GetRandom function made from the preceding clause is used, favorite random numbers can be calculated.
However, the problem that should be thought has been left actually still.
The quasi-random number is due to the calculation as explained in the beginning.
In a word, it becomes the same random numbers when making it based on the same number.
Execute the program made in the preceding clause twice to confirm this.
The first
1
3
1
3
2
4
2
5
6
2
Very, quite the same value is obtained by the first time and the second times. It is the same no matter how it does.
The second
1
3
1
3
2
4
2
5
6
2
It is necessary to change former number used to calculate random numbers to solve this problem.
The srand function is prepared as a function for that.
However, even if you put another numerical value by using the srand functionsrand(元の数);
In short, want to put ..[detarame].. [suu] somehow or other completely in the srand function, and
They one fit method of are. It is a method of putting time now.
If time now in every the second is put in the srand function, former every time different number can be used for random numbers.
The function that obtains time now : in the time function # include should do < time.h >.
If the srand function and the time function are used as follows, it is ..every time different random numbers.. computable.
The usage of the time function is neither known, not cared about, and make it to this street in case of random number generation.srand((unsigned int)time(NULL));
The result of executing this program twice might become as follows.#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 first
6
6
5
4
6
5
1
3
2
6
It is understood to obtain a splendidly different value.
The second
5
2
2
4
3
5
4
1
3
1
Though the srand function is used here by the main function
If as follows is done, this can be included in the GetRandom function.
Because the static variable is used, the srand function is used only once.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)); }