C # random number generator

Source: Internet
Author: User
Tags random seed

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);++

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.