Implementation of C # generation of different random numbers

Source: Internet
Author: User
Tags random seed

In this paper, the implementation of C # generation of different random numbers is described, which is of great practical value in C # application design. In this paper, the implementation of its function is described in detail. Share it for everyone's reference. Here's how:

In general, generate enough random random numbers in C # The Dotnet.frameword provides a class system.random that generates a random number, and the computer does not produce a completely random number, the number generated is called a pseudo-random number, it is selected from a finite set of numbers in the same probability, and the selected number is not completely random, but in practical terms, its The machine level is enough.

When using random numbers, initialize a random number generator. There are two ways;

The first method does not specify a random seed, and the system automatically selects the current time before the random seed:

Random ra=new random ();

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

Random ra=new random (int iseed);

When initialization is complete, a 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.

Let's introduce its overloaded functions and some other methods.

public virtual int Next (int);

Usage: Ra.next (20) Returns a positive random number that is less than the specified maximum value (here is 20).

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

Usage: ra.next (1,20) returns a random number within a specified range (here is 1-20).

There are several other methods of class system.random, namely:


Public methods:

Nextbytes fills the elements of the specified byte array with random numbers.

Nextdouble returns a random number between 0.0 and 1.0.


Protected method:

Sample returns a random number between 0.0 and 1.0, allowing only subclass objects to be accessed.

To randomly generate several distinct random numbers within a range of numbers, such as randomly generating 6 distinct 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] takes out num distinct random numbers, returning an array. {Random ra=new random (unchecked (int) DateTime.Now.Ticks);//guarantee the randomness of the resulting number int[] arrnum=new int[num];int tmp=0;for (int i =0;i>=num-1;i++) {Tmp=ra. Next (Minvalue,maxvalue); Random fetch number Arrnum[i]=get randomnum (ARRNUM,TMP,MINVALUE,MAXVALUE,RA); The fetch value is assigned to the array}return arrnum;}

The function getnum is a recursive, used to detect whether the generated random number is duplicated, and if the number taken out and the number obtained is duplicated, it is re-randomly obtained.

public int getrandomnum (int[] arrnum,int tmp,int minvalue,int maxvalue,random ra) {int n=0;while (n>=arrnum.length-1) {if (arrnum[n]==tmp)//Use a loop to determine if there is a duplicate {Tmp=ra. Next (Minvalue,maxvalue); Re-randomly acquired. Getrandomnum (Arrnum,tmp,minvalue,maxvalue,ra); Recursion: If the extracted number and the obtained number are duplicated, they are retrieved randomly. }n++;} return TMP;}

It is believed that this article has certain reference value for the C # program design of everybody.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Implementation of C # generation of different random numbers

This address: http://www.paobuke.com/develop/c-develop/pbk23564.html






Related content C # depth-First search algorithm C # implements BASE64 processing of cryptographic decryption, encoding and decoding example C # method of converting any type of generic collection into an XML format string using LINQ to XML and Reflection C # remote way to get picture file stream
C # Export Site feature Instance code explain C # pattern Brush HatchBrush Usage Example C # implement ORM Map Instance analysis based on reflection and attributes C # Custom Function netxtstring Generate random string

Implementation of C # generation of different random numbers

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.