How to create an array of non repeating random numbers

bIG_aL

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;
    }





}
Pierre-Luc Pineault

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);
} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Assigning random numbers in an array in java (non repeating)

From Dev

non-repeating random numbers

From Dev

Non-repeating random numbers inside array JAVA

From Dev

How to create an array of Random numbers?

From Dev

How to generate random numbers with non-repeating digits in Java?

From Dev

How can I create a List of random int numbers without repeating?

From Dev

Generating non-repeating random numbers in JS

From Dev

python - generating a non repeating random pairs of numbers

From Dev

python - generating a non repeating random pairs of numbers

From Java

Create an Array with random numbers

From Dev

How do I generate non-repeating random numbers in a while loop? (Python 3)

From Dev

How to generate 4 random non-repeating numbers from 0-9?

From Dev

Repeating numbers random Android studio java random array problem

From Dev

Repeating random numbers in VBA

From Dev

Repeating random numbers in VBA

From Dev

How do I generate 2d array table with non-repeating numbers?

From Dev

Random numbers in non Initialized array C++

From Dev

How to create non-repeating Random String of size 5 using special characters (including space) only using java?

From Dev

shortest code to create an array of random numbers in swift?

From Dev

Identifying repeating numbers in a array

From Dev

Is there a way to create an array in java with some initialized numbers and then add random numbers to it

From Dev

How to create a random mask array?

From Dev

How to create random numbers a specific number of times?

From Dev

How to fill an array with random numbers and freeze/safe the numbers inside of the Array

From Dev

How do I populate an array with random numbers?

From Dev

How can i add random numbers in an array?

From Dev

How to multiply array of ones by random numbers

From Dev

Replacing a repeating number in a file with random numbers

From Dev

JavaScript generate random numbers without repeating

Related Related

  1. 1

    Assigning random numbers in an array in java (non repeating)

  2. 2

    non-repeating random numbers

  3. 3

    Non-repeating random numbers inside array JAVA

  4. 4

    How to create an array of Random numbers?

  5. 5

    How to generate random numbers with non-repeating digits in Java?

  6. 6

    How can I create a List of random int numbers without repeating?

  7. 7

    Generating non-repeating random numbers in JS

  8. 8

    python - generating a non repeating random pairs of numbers

  9. 9

    python - generating a non repeating random pairs of numbers

  10. 10

    Create an Array with random numbers

  11. 11

    How do I generate non-repeating random numbers in a while loop? (Python 3)

  12. 12

    How to generate 4 random non-repeating numbers from 0-9?

  13. 13

    Repeating numbers random Android studio java random array problem

  14. 14

    Repeating random numbers in VBA

  15. 15

    Repeating random numbers in VBA

  16. 16

    How do I generate 2d array table with non-repeating numbers?

  17. 17

    Random numbers in non Initialized array C++

  18. 18

    How to create non-repeating Random String of size 5 using special characters (including space) only using java?

  19. 19

    shortest code to create an array of random numbers in swift?

  20. 20

    Identifying repeating numbers in a array

  21. 21

    Is there a way to create an array in java with some initialized numbers and then add random numbers to it

  22. 22

    How to create a random mask array?

  23. 23

    How to create random numbers a specific number of times?

  24. 24

    How to fill an array with random numbers and freeze/safe the numbers inside of the Array

  25. 25

    How do I populate an array with random numbers?

  26. 26

    How can i add random numbers in an array?

  27. 27

    How to multiply array of ones by random numbers

  28. 28

    Replacing a repeating number in a file with random numbers

  29. 29

    JavaScript generate random numbers without repeating

HotTag

Archive