C # generate random numbers with different numbers

Source: Internet
Author: User
Tags random seed

Dotnet. frameword provides a special random generation class System. random, a computer cannot generate completely Random numbers. The numbers it generates are called pseudo-Random numbers. They are selected from a group of finite numbers with the same probability, the selected number is not random, but its degree of randomness is sufficient.

When using a random number, you must first initialize a random number generator. There are two methods;

The first method does not specify a random seed. The system automatically selects a Random Seed before the current time:

Random ra = new Random ();

The second method is to specify an int type parameter as a random seed:

Random ra = new Random (int iseed );

After initialization, the Random number is generated using the Random. Next () method.

Ra. Next (); it returns a number greater than or equal to zero and less than 2,147,483,647.

Next we will introduce its overload functions and other methods.



Public virtual int Next (int );

Usage: ra. next (20)

Returns a positive random number that is less than the specified maximum value (20 here.



Public virtual int Next (int minValue, int maxValue );

Usage: ra. next (1, 20)

Returns a random number in the specified range (between 1 and 20.



There are several methods for the System. Random class:

Public method:

NextBytes uses a random number to fill in the elements of the specified byte array.

NextDouble returns a random number between 0.0 and 1.0.



Protected methods:

Sample returns a random number between 0.0 and 1.0, which can only be accessed by subclass objects.

You must randomly generate several random numbers in a given number range. For example, you can randomly generate six random integers from 1 to 20.

You can refer to the following two functions:

DifferSamenessRandomNum and getRandomNum:



Public int [] differSamenessRandomNum (int num, int minValue, int maxValue)

// In the interval [minValue, maxValue], retrieve num random numbers that are different from each other and return an array.

{



Random ra = new Random (unchecked (int) DateTime. Now. Ticks); // ensure the randomness of the generated number

Int [] arrNum = new int [num];

Int tmp = 0;

For (int I = 0; I <= num-1; I ++)

{

Tmp = ra. Next (minValue, maxValue); // random count

ArrNum [I] = get RandomNum (arrNum, tmp, minValue, maxValue, ra); // The retrieved value is assigned to the array.



}

Return arrNum;

}

The getNum function is recursive. 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.

Public int getRandomNum (int [] arrNum, int tmp, int minValue, int maxValue, Random ra)

{

Int n = 0;

While (n <= arrNum. Length-1)

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.