Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

c# - How to create an array of non repeating random numbers

I have a lottery application in C# which takes in the number of numbers to draw and also the maximum number to draw.I have coded up to creating an array holding the required random numbers but I need them to be unique and am having trouble doing it.I would be very grateful if someone could give me some advice on this,Thanks

Here is my code so far:

class Lottery

{

  static int[] numberHolder; //array to be filled with numbers up to an 
                             //amount entered by the user eg 42 Max

  static int[] drawHolder;   //array to hold the each random number 
                             //drawn from the pool of numbers eg 7 numbers

    public Lottery() //Lottery class Constructor
    {

    }


    //method which takes in a number limit and amount of numbers to be drawn
    public String drawNumbers(int numLimit, int numAmount) 
    {

        Random RandomNumber = new Random();

        for (int i = 0; i < numLimit ; i++) //loop to fill up numberHolder array
                                            // with predefined limit of numbers

        {
            numberHolder[i] = i++;
        }

        for (int i = 0; i < numAmount; i++)
        {


            // code to pick unique random numbers no greater than numAmount 
            // and add them to the drawHolder[] array
            drawHolder[i] = RandomNumber.Next(1, numLimit);

        }





        //return the drawHolder array to String
        return null;
    }





}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In my opinion, you should change your approach.

Instead of thinking "I'll generate random indexes to pick my numbers", where you have to be sure you don't get any duplicates, I would simply shuffle the array and take the X first you need. That way, you don't need to worry about indexes or duplicates.

So your second for loop would be changed to

drawHolder = numberHolder.OrderBy(x => new Guid()).Take(numAmount);

(Please note I've used new Guid() so you can remove your RandomNumber declaration line. As discussed before, a GUID is a unique value and not meant for to be used as a random gen. You could also use x => RandomNumber.Next(), but if you really need a strong and reliable shuffe, read about Fisher-Yates)

You can also replace your numberHolder array with a simple Enumerable.Range

So your whole code would become (And please note I've changed your method name to use C# conventions, method names should be in PascalCase)

public string DrawNumbers(int numLimit, int numAmount) 
{
    drawHolder = Enumerable.Range(0, numLimit).OrderBy(x => new Guid()).Take(numAmount);

    return string.Join(", ", drawHolder);
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...