asp.net random numbers with no duplicates

Source: Internet
Author: User

protected void Page_Load (object sender, EventArgs e)
{
Set_random ();
}
<summary>
A method of producing a non-repeated random number
</summary>
<param name= "MinValue" > Min </param>
<param name= "MaxValue" > Max </param>
<param name= "Count" > Total number of random numbers generated </param>
<returns>int[] Array </returns>
Public int[] Getrandomunrepeatarray (int minvalue, int maxValue, int count)
{
Random rnd = new Random ();
int length = Maxvalue-minvalue + 1;
Byte[] keys = new Byte[length];
Rnd. Nextbytes (keys);
int[] items = new Int[length];
for (int i = 0; i < length; i++)
{
Items[i] = i + minvalue;
}
Array.Sort (keys, items);
Int[] result = new Int[count];
Array.copy (items, result, count);
return result;
}
public void Set_random ()
{
The number of data generated by a method that generates random numbers is a random number from 1111111 to 9999999 output of 10
int[] items = this. Getrandomunrepeatarray (1111111, 9999999, 10);
for (int i = 0; i < items. Length; i++)
{
Response.Write ("The resulting random number is:" + items[i] + "<br/>");
}
}

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.