1
I’m working on a Bingo in C# for a facul work and need to make a vector of so many values and make it never repeat this values, I am using the following procedure to generate the values of draw ↓
public static void GeraBingo(int[] v)
{
Random r = new Random();
for (int i = 0; i < v.Count(); i++)
{
v[i] = r.Next(1, 10); // gera 1 a 10 e vai gerar quantos o v.Count() mandar
Thread.Sleep(1000); // pausa 1000 miliseg.
}
}
This code generates the vector, but I’m not able to assimilate a method not to enter repeated.
With this code above I get an array like this for example: |9 |2 |5 |6 |5 |4 |2 |4 |6 |5 | and how visible it repeats some values. This method is for working with only an int Array of values and not letting repeat values.
A suggestion would be to use another structure Hashset, the Add method, returns
false
when you enter a non-unique value.– Gabriel Gonçalves
The Yates shuffer is really a solution to this problem. But its problem may be a little different from what the bigown marked as duplicate. Please give more details, including answer the following question: Do you want to generate all the numbers or only part of them? If the answer is part of them, this is a different question.
– Bruno Costa
Moderators do as they please around here. One question sounds like the other.
– Guilherme HS