C # generate code instances without repeated random numbers

Source: Internet
Author: User

You can consider placing these 100 numbers in a single number group and randomly taking a location (the first time is 1-100, the second time is 1-99 ,...), you can compare the number of the position with the number of the last position.

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
Int [] arr = new int [101];
Int I;
// Initialize the Array
For (I = 1; I <= 100; I)
{
Arr [I] = I;
}
// Random Number
Random r = new Random ();
For (int j = 100; j> = 1; j --)
{
Int address = r. Next (1, j );
Int tmp = arr [address];
Arr [address] = arr [j];
Arr [j] = tmp;
}
// Output
Foreach (int k in arr)
{
Console. Write (k "");
}
}
}
}

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.