0
int[] RandArray = new int[6];

        Random randNumber = new Random();
        for (int Counter = 0; Counter < RandArray.Length; Counter++)
        {
            RandArray[Counter] = randNumber.Next(1,50);

        }

        Console.WriteLine(RandArray[0]);
        Console.WriteLine(RandArray[1]);
        Console.WriteLine(RandArray[2]);
        Console.WriteLine(RandArray[3]);
        Console.WriteLine(RandArray[4]);
        Console.WriteLine(RandArray[5]);
        Console.ReadLine();

This program generates 6 Random numbers from 1 to 49. Presently, it generates the same numbers twice. How do I make it not duplicate any number? And can this code be improved?

3
  • codereview.stackexchange.com/questions/61338/… Commented Oct 3, 2015 at 18:33
  • Thank you for quick responds. I forgot to ask how can I put the final list of numbers in an ascending order? Commented Oct 3, 2015 at 18:58
  • @HaseebAhmed, I updated my answer to show how to order the list. Commented Oct 3, 2015 at 19:50

4 Answers 4

2

You can use a HashSet to store the numbers that you have generated so far. The HashSet will only add the newly generated number if it does not contain the number already. Like this:

Random rand_number = new Random();

HashSet<int> numbers = new HashSet<int>();

while (numbers.Count < 6)
{
    int new_number = rand_number.Next(1, 50);

    numbers.Add(new_number);
}

You can use a List instead of the HashSet if you want, but a HashSet will perform sigificantly better if the number of integers to generate is big.

You can convert the result into an array after you finish (if you want), like this:

int[] integer_array = numbers.ToArray();

Instead, you can do this if you want the numbers to be ordered ascending:

int[] integer_array = numbers.OrderBy(x => x).ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

you dont need the Contains() as the int wont get added if it's already in the set.
1

This answers your title question.
Use Distinct LINQ operator, it will remove the duplicate elements https://msdn.microsoft.com/en-us/library/vstudio/bb348436(v=vs.100).aspx

This answers your body question
To not generate same number multiple times store the numbers in a HashSet and check each iteration if number was generated previously and re-roll if needed.

Comments

1

You could create a range of numbers from 1 to 50, order them randomly and then 6 from that range into a list:

var rnd = new Random();
var randomNumbers = Enumerable.Range(1,50).OrderBy(x => rnd.Next()).Take(6).ToList();

.NET Fiddle - https://dotnetfiddle.net/i06zCY

Comments

0

You can check for a duplicate for each number that you create:

int[] RandArray = new int[6];

Random randNumber = new Random();
for (int Counter = 0; Counter < RandArray.Length; Counter++) {
  // Loop until you find an unused number
  bool found;
  do {
    // Get a random number
    int num = randNumber.Next(1,50);
    // Look through the numbers picked so far
    found = false;
    for (int i = 0; i < Counter; i++) {
      found |= num == RandArray[i];
    }
    // If the number was found go back and pick a new one
  } while(found);
  // Put the number in the array
  RandArray[Counter] = num;
}

foreach (int n in RandArray) {
  Console.WriteLine(n);
}
Console.ReadLine();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.