Generating and storing unique and random numbers within a range

Awani

I tried the following code to generate random numbers and store them in a HashSet in c#:

class program
{
  static void Main(String[] args)
  {
    Random r=new Random();
    HashSet <int> h = new HashSet<int>();
    for (int j=0;j<9;++j)
    {
  h.Add(r.Next(9));
    }
    int [] array = h.ToArray();
    foreach(int i in array)
  Console.WriteLine(i);
    Console.ReadLine();
   }
}

Each time I execute the program, the number of elements being displayed differs. Since I'm using a loop to store 9 values, I expect 9 values to be displayed, which is not happening. What could be the possible error?

Selman Genç

HashSet doesn't contain duplicates.Your loop runs 9 times but only unique numbers are added to the HashSet.Add your numbers directly to your array and you should see 9 numbers displaying. Or use a wider range to generate random numbers.Btw you can verify that how many numbers in the HashSet like this, after the for loop:

Console.WriteLine(h.Count);

Alternatively you can change your for loop like this:

for (int j = 0; j < 9; ++j)
{
    if(!h.Add(r.Next(9))) j--;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Generating random unique BigIntegers within a specified range?

Generating random numbers within a specified range

Generate 'n' unique random numbers within a range

Generating unique random numbers

Generating random numbers which allows some repetition within a given range

Generating random evenly divisible numbers within a specific range in C++?

generating unique random numbers in Julia

Generating Unique Random Numbers in Java

Generating array of unique random numbers

Generate Array of Unique Random Numbers within Inclusive Range

Generating random dates within a given range in pandas

Generating random dates within a given range in pandas

Query that generates random unique numbers within a specific range for columns within the same row

Generating random numbers unique from another variable

Generating unique random numbers and check duplicates

Generating random numbers in specific binary range

Generating random whole numbers in JavaScript in a specific range?

Generating random numbers over a range in Go

Generating random integer and real numbers in a given range

Java: Generating a random double within a range (inclusive of the min and max to the range)

Javascript unique random number generator is not generating unique numbers

Generate 'n' unique random number within a range

Generating random Numbers , but it must be Generated with Unique numbers without replications

Generate random numbers, letters or characters within a range

replace NA in a dataframe with random numbers within a range

Printing Non Repeating Random Numbers within a range

how to generate unique random numbers with a specific range

Maximize the number of unique random numbers in a range

Pushing unique numbers within a range to an array in Javascript