Generate random numbers that are not repeated

Source: Internet
Author: User

Although I have seen other people encounter this problem before, I can leave it empty.

However, I recently thought about how to find a solution, so I didn't check the information.

Let's talk about my personal ideas. I think it's really a tough start. For example, I want to generate two random numbers that do not repeat between 1 and 5.

If 3 is generated for the first time, then the number X generated for the second time determines if this X has already appeared. If so, try again.

Random rnd = <> lstResult =  List<>(lstResult.Count < ){             temp = rnd.Next(,(lstResult.Contains(temp))            

( i = ;i < ;i++)

Then a random number is generated.

 rndNum = rnd.Next(i ,);

Use rndNum as the index of the arrIndex array.

Exchange two numbers, arrIndex [I] And arrIndex [rndNum ].

Random Number rndNum = rnd. next (,) <= [] <and I (secondary random number rndNum = rnd. next (,) <= [] <and I (secondary random number rndNum = rnd. next (,) <= [] <and I (secondary random number rndNum = rnd. next (,) <= [] <and I (secondary random number rndNum = rnd. next (,) <= [] <and I (

In the above step, two 7 numbers are generated from the random number, but if we use this 7 as an index to disrupt an ordered set, then we can retrieve the x on the left as the random number we want.

Code:

[] arrNums =  [ ( i = ; i < ; i++= i + =  ( i = ; i < ; i++ index = rnd.Next(i,  temp ===

There are many ways to generate non-repeated random numbers on the Internet. For more information, I just came up with what I did at the time. reference ,,,

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.