Randomly generated numbers with arrays are repeated

Asked

Viewed 85 times

0

I looked everywhere but I couldn’t find anything that would solve my problem. At the moment I have this:

int[] nums5 = new int[100];

        for (int i = 0; i < nums5.Length; ++i)
        {
            int aleat5;
            do
            {
                aleat5 = r.Next(1, 99);
            } while (nums5.Contains(aleat5));
            lblNum.Text = aleat5.ToString();

However, although random numbers appear, they repeat themselves. How do I fix this?

  • 1

    What is the final result you want? An array with 100 different numbers?

  • I am making a Bingo game and this array serves for the numbers that will be generated automatically. I mean, I want 100 randomly generated numbers to appear and none of them to be repeated!

  • 1

    You can fill the array with the loop index (nums5[i] = i + 1) and then shuffle the array using this algorithm: https://stackoverflow.com/a/110570/5775775. This will generate an array with numbers from 1 to 100 shuffled.

  • I had already seen this code and had not reached that result... I will try again

  • 1

    See the code working here: https://repl.it/repls/SameFrequentBlogclient

  • 1

    Another possible solution is for you to store the numbers generated in an array and only accept new numbers generated since they have not been previously entered in the array.

  • That was my initial idea. Only later I could not put the numbers of the array in a label

Show 2 more comments

2 answers

2


As I commented, you can fill the array with the loop index (nums5[i] = i + 1) and then shuffle the array using this algorithm: https://stackoverflow.com/a/110570/5775775. This will generate an array with numbers from 1 to 100 shuffled.

The code would look like this:

int[] numeros = new int[100];

// Preenche o array com os numeros de 1 a 100
for (int i = 0; i < numeros.Length; i++) {
  numeros[i] = i + 1;
}

// Embaralha os numeros (https://stackoverflow.com/a/110570/5775775)
Random rng = new Random();
int n = numeros.Length;
while (n > 1) {
    int k = rng.Next(n--);
    int temp = numeros[n];
    numeros[n] = numeros[k];
    numeros[k] = temp;
}
  • But how do I then put the numbers on the label? I can’t do lblNum.text = numbers; or lblNum.text = .toString numbers();

  • Usa string.Join(", ", numeros)

  • and it is possible to make the generated numbers between 1 and 100?

  • They’re giving me numbers too big

  • 1

    Calculate the rest of the division by 100 and add 1. (operator %)

1

An idea would be to use a List<int> or Queue<int> to store the numbers instead of an array, because later Voce needs to remove the "chosen element"..

With a Queue<int> that it would be something like this:

Declare the Queue out of a method/function, usually before public Form1()

Queue<int> aleatorios;

Fill the form with values, in the place where you initialize the variables or in the form constructor (inside public Form1(){})

var rnd = new Random();
aleatorios = new Queue<int>(Enumerable.Range(1, 100).OrderBy(i => rnd.Next()));

Assign a value of the label (because it will be the first element), ie, make a Dequeue...

lblNum.Text = aleatorios.Dequeue().ToString();

the Dequeue automatically removes the first element of queue which has been assigned to the label

  • Even though I thought it would look good, the numbers are repeating

  • http://prntscr.com/mfqv56 as you can see from the print

  • 1

    print out the code

  • http://prntscr.com/mfqw7o

  • 1

    You have to initialize the form constructor inside: public Form1( { // randoms = new Match etc.... } as I wrote in the answer... or within a method/function that initializes all variables/controls... but also call this function in the form constructor...

  • 1

    I got it, I didn’t get a good look at what I said, I’m sorry!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.