How to create an array of non repeating random numbers

random number generator that doesn't repeat java
unique random number generator
unique random number generator in java example
random number generator arraylist java
how to generate random numbers in java within range
generate random number without repetition
java random number
random array no repeats java

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;


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

How To Generate Non Repeating Random Numbers from 1 to 49 , How do you generate a non repeating random number in Java? Bottom line: Learn how to create a list of unique random numbers with no duplicates or repeats. Skill level: Intermediate My friend and fellow blogger, George Mount, posted a question on Facebook yesterday about creating a list of random non-repeating numbers.

There are a few options, you could use the Contains method.


The Any() method.

numberHolder.Any(x=>x == value);

Generate Non-Repeating Random Number From A Set, How do I select random names from a list without repetition in Excel? I'm trying to build a Powerball randomizer and I'm nearly finished. I need help with getting the first 5 numbers to no repeat themselves (the 6th, PowerBall number can repeat). I'm not sure how to tackle it as I'm still pretty new to writing code. Any suggestions and tips on how to do it and/or how to improve my current code is welcome.

Sounds like shuffling your array is better than generating random numbers. You could do it like this:

int[] ShuffleArray(int[] array)
  Random r = new Random();
  for (int i = array.Length; i > 0; i--)
    int j = r.Next(i);
    int k = array[j];
    array[j] = array[i - 1];
    array[i - 1]  = k;
  return array;

Credit goes to @RohitArora.

How to generate random numbers between 0 and 10 with no repeat , Does anybody know how to generate a series of 10 non-repeating random integers between 1 and 49? So far I've tried using p = randperm(50); p = p(1:10)-​1; but  Imagine that you want to generate 3 random numbers (numGuesses = 3). Now let's say that the random numbers generated are 5, 2, 5. The last 5 won't be added to your Set. Hence the Set contains only 5 and 2. When you will construct the array to be returned (of length 3), the last element of the array will be fill up with a null value.

This is easily achieved using a shuffling algorithm on an array of non-repeating integers:

public int[] GenerateNonRepeatingNumbers(int seed, int min, int range)
        // Make sure range is an appropriate value
        if (range <= 0)
            throw new ArgumentException("Range must be greater than zero.");

        // Make an array to hold our numbers
        int[] numbers = new int[range];

        // Seed the RNG.
        Random rng = new Random(seed);

        // Fill the array with all numbers from min to min + range
        for (int i = 0; i < range; numbers[i] = min + i++) { }

            a = 0, // Swap index
            t = 0; // Temporary value storage

        // Scramble the values
        for (int i = 0; i < range; i++)
            // Get a random index that isn't i
            while ((a = rng.Next(range)) == i) { };
            // Store the old value at i
            t = numbers[i]; 
            // Change the old value to the value at the random index
            numbers[i] = numbers[a]; 
            // Set value at random index to our old value from numbers[i]
            numbers[a] = t; 

        return numbers;

How to Create a List of Random Numbers with No Duplicates or , It is used for removing a number from the set array. Basically every time a unique number is generated it is removed from the set. But to remove it  This will return an array of numbers from 1 through 10 in random order. You just need to add the following to the head of your html document, and you can do pretty much whatever you want with randomness easily. Random values from arrays, random jquery elements, random properties from objects, and even preventing repetitions as I've shown here.

I would keep it simple:

private Random _rnd = new Random();
public String drawNumbers(int numLimit, int numAmount) 
    return String.Join("," ,
            .Range(1, numLimit)
            .OrderBy(x => _rnd.Next())

Keep the declaration of the Random instance outside of the method to prevent returning identical sequences if you call the method in quick succession.

How to generate non-repeating random numbers in Python?, [code]static void Main(string[] args) { var numbers = new List<int>(); var How do I generate random numbers between 0 and 10 with no repeat any value, in java List<Integer> numbers = new ArrayList<Integer>(10);; for (int i = 0; i < 10; i++)  How To Generate Non Repeating Random Numbers Learn more about randperm, non-repeating, random numbers

Generating Unique Random Numbers Using Java, Bottom line: Learn how to create a list of unique random numbers with no The SEQUENCE Function returns a list (array) of numbers. How to generate non-repeating random numbers in Python? PythonServer Side ProgrammingProgramming. Following program generates 10 random, non-repetitive integers between 1 to 100. It generates a random integer in the given interval and adds it in a list if it is not previously added. >>> import random >>> list= [] >>> for i in range (10): r=random.randint (1,100) if r not in list: list.append (r) >>> list [13, 53, 25, 95, 64, 87, 27, 93, 74, 60]

Creating an array of random numbers with no duplicates, Following program generates 10 random, non-repetitive integers between 1 to 100. It generates a random integer in the given interval and  If your pool of numbers did have a repeat value, each value would be considered unique in this process. So if the number 9 was repeated 3 times, this process would generate 9 three times as well, but each 9 would be considered its own value. You could use this process for letters, strings, doubles, or even pointers to objects.

generate random number in java within a range without repeating , Generating Unique Random Numbers. An ArrayList and the Shuffle method simulate a sequence with no repeats. Share; Flipboard; Email. Suppose you need to generate random numbers without duplicates into column A and column B, now select cell E1, and type this formula =RAND (), then press Enter key, see screenshot: 2. And select the whole column E by pressing Ctrl + Space keys simultaneously, and then press Ctrl + D keys to apply the formula =RAND () to the whole column E.

  • thank you for this its very helpful,can you explain what Enumerable.Range() is/does ,sorry I'm new to c#
  • @bIG_aL I encourage you to read the MSDN page for it. The doc is really well done and pretty informative. But to be brief, it does exactly what your first loop is supposed to be doing, but with a starting value. It generates an array from starting value up to starting value + count.
  • Somewhat risky sorting with an unstable key, better to do Enumerable.Range(1, limit).Select(i => new { g=Guid.NewGuid(), i }).OrderBy(e => e.g).Select(e => e.i);
  • Guids aren't guaranteed to be random - they're unique. I wouldn't rely on using Guids as you never know if the implementation is actually random or not - and it could change in the future making programs that rely on the randomness fail.
  • @Enigmativity Of course, and it was discussed multiple times (and there's even a specific question here). I'll add even more italic text haha, thanks :)
  • Generating random numbers only once to get them all in a range could be reaaaaaly long
  • You're right, it's begging for someone to specify a range that's smaller than the number of selections and loop forever.