C # randomly generates several distinct random numbers within a range of numbers

Source: Internet
Author: User
Tags random seed

<summary>
Random ra=new random (); The system automatically selects the current time before making a random seed:
Random ra=new Random (6) specifies a parameter of type int as a stochastic seed;
Ra. Next (); Returns a random number greater than or equal to zero and less than 2,147,483,647
Ra. Next (20); Returns a random number that is greater than or equal to zero and less than 20
Ra. Next (1,20); Returns a random number greater than or equal to 1 and less than 20
The following function returns a positive integer that is greater than or equal to a positive integer (with 0) and less than or equal to a positive whole number.

Example int[] a= getrandomnum (12,1,100); Randomly pick 12 different numbers in 1-100 to coexist in array a
Come from one place.
</summary>

Public int[] getrandomnum (int num, int minValue, int maxValue)
{
if ((MaxValue + 1-minvalue-num<0))
MaxValue + = num-(maxValue + 1-minvalue);
Random RA = new Random (unchecked ((int) DateTime.Now.Ticks));
int[] Arrnum = new Int[num];
int tmp = 0;
StringBuilder sb=new StringBuilder (num *maxvalue. ToString (). Trim (). Length);

for (int i = 0; I <= num-1; i++)
{
TMP = RA. Next (MinValue, MaxValue);
while (SB.) ToString (). Contains ("#" + tmp.) ToString (). Trim () + "#")
TMP = RA. Next (MinValue, maxvalue+1);
Arrnum[i] = tmp;
Sb. Append ("#" + tmp.) ToString (). Trim () + "#");
}
return arrnum;
}

C # randomly generates several distinct random numbers within a range of numbers

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.