We often need to randomly generate a group of non-repeated questions when performing an exam system that can automatically generate the exam. net Framework provides a class System dedicated to generating random numbers. random.
As we all know about random numbers, computers cannot generate completely random numbers. The so-called C # random number generator uses certain algorithms to perform complex operations on the randomly selected seeds, use the generated results to simulate a completely random number, which is called a pseudo-random number. A pseudo-random number is selected from a finite number with the same probability. The selected number is not completely random, but from a practical point of view, the random degree is enough. The selection of pseudo-random numbers starts with the random seed. To ensure that each pseudo-random number obtained is "random" enough, the selection of Random Seed is very important. If the random seed is the same, the random number generated by the same C # random number generator will also be the same. Generally, we use parameters related to system time as random seeds, which is also the default method used by the C # random number generator in. net Framework.
We can initialize a C # random number generator in two ways:
The first method does not specify the random seed. The system automatically selects the current time as the Random Seed:
Random ro = Random();
The second method can specify an int type parameter as a random seed:
iSeed== Random( tick == Random(()(tick & ) | () (tick >> ));
It is used to check whether the generated random number is repeated. If the obtained number and the obtained number are repeated, the random number is obtained again.
Random ra= Random((([] arrNum= [ tmp= minValue= maxValue= ( i=;i<;i++=ra.Next(minValue,maxValue); arrNum=getNum(arrNum,tmp,minValue,maxValue,ra); getNum([] arrNum, tmp, minValue, n= (n<=arrNum.Length- (arrNum[n]==tmp) =ra.Next(minValue,maxValue); getNum(arrNum,tmp,minValue,maxValue,ra);++