※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Random numbers   

  1. ..clause 1..: Quasi-random number
  2. ..clause 2..: Make random numbers.
  3. ..clause 3..: Limit the range of random numbers.
  4. ..clause 4..: Make it to an every time different random numbers.

[1] Quasi-random number

Random numbers are random as its name suggests numbers.
In short, think it is the same as dice.

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.


[   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.
In the quasi-random number, the number that the calculation looks random to the last is made.
However, because actually considerably various numerical values can be obtained
I think that you may think that it is an almost random number.

[ 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.

It returns to contents.


[2] Make random numbers.

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 >.


変数 = rand();
The parameter etc. need not be especially passed to the rand function.
Calculate a random value only by using it as it is.
The following program is an example of calculating random numbers ten times.
 
#include <stdio.h> #include <stdlib.h> 

int main(void)  
{
	int i;  
	
	for (i = 0;i < 10;i++) { 
		printf("%d\n",rand()); 
	}
		
	return 0;
}
The execution result of this program might become as follows.

130
10982
1090
11656
7117
17595
6415
22948
31126
9004
As you see, a random value is obtained.

It returns to contents.


[3] Limit the range of random numbers.

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   ]
Minimum value + (int)( rand() * (maximum value-minimum value + 1.0) / (1.0 + RAND_MAX))
Do not understand and do not care about this official meaning.
It is ..random numbers within the range of the maximum minimum value-value.. computable if it makes it to this street anyway.
The following program makes the GetRandom function that calculates the formula above.
It is an example of shaking dice ten times by using the GetRandom function.
 
#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 execution result of this program might become as follows.

1
3
1
3
2
4
2
5
6
2
As you see, a random number within the range of 1-6 is obtained.

[   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.

It returns to contents.


[4] Make it to an every time different random numbers.

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

The second
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.
Instead, it doesn't become it about dice though no very [haari] with this.

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.


srand(元の数);
However, even if you put another numerical value by using the srand function
It doesn't become a solution because it becomes the same random numbers if former number is the same when executed.
Of course, because random numbers made in the beginning are the same even if the rand function is put in the srand function, it is meaningless.
It is an unkind time-consuming means though there is a method of making the user input it, too.

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.

 srand((unsigned int)time(NULL));
The usage of the time function is neither known, not cared about, and make it to this street in case of random number generation.
This is an integral value in the type named unsigned int without the sign though it is Cast.
This processing is enough if it does once when the program begins.
The following program is an example of calculating an every time different add the above-mentioned processing random numbers.
 
#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 executing this program twice might become as follows.

The first
6
6
5
4
6
5
1
3
2
6

The second
5
2
2
4
3
5
4
1
3
1
It is understood to obtain a splendidly different value.

Though the srand function is used here by the main function
If as follows is done, this can be included in the GetRandom function.

 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));
}
Because the static variable is used, the srand function is used only once.
The speed slows though it becomes random numbers even if the srand function is used every time.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.