How to generate an array using Random to assign values, but make it never repeat values?

Asked

Viewed 2,820 times

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.

  • 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.

  • Moderators do as they please around here. One question sounds like the other.

2 answers

1

you can use an auxiliary array with all available values.:

public static int[] CriarLista(int min, int max) 
{
    var lista = new int[max - min + 1];
    for (var valor = min; valor <= max; valor++)
        lista[valor - min] = valor;
}

public static void GeraBingo(int[] v)
{
    Random r = new Random();
    var valores = CriarLista(1, 10);
    for (int i = 0; i < v.Count(); i++)
    {
        var indice = r.Next(1, 10) - 1;
        var valor = valores[indice];
        valores[indice] = valores[0];
        valores[0] = valor;

        v[i] = r.Next(1, 10); // gera 1 a 10 e vai gerar quantos o v.Count() mandar
        Thread.Sleep(1000);  // pausa 1000 miliseg.
    }
}

1

It seems to me what you want is to place the numbers one through ten in a ten position vector, randomly. If that’s right, what you need is an auxiliary structure from which the numbers will be drawn at random similar to the bingo machine from which we get the numbers.

private int[10] _vetorResultado;
private List<int> _todosOsNumeros;

public int[] GeraBingo()
{
    Random r = new Random();
    for (int i = _vetorResultado.Length; i >= 0; i--)
    {
        _todosOsNumeros.add(i);
    }

    for (i = 0; i < _vetorResultado.length; i++)
    {
        int bolaDaVez = _todosOsNumeros[r.Next(0, _todosOsNumeros.Count - 1)];

        _vetorResultado[i] = _todosOsNumeros.Remove(boladaVez); // Isso reduz o tamanho da fila de números pendentes
    }
    return _vetorResultado;
}

Note that you can change the size of the structure that the algorithm keeps running in the same way.

Note also that the method Remove from the generic list not only removes the item but also returns the value removed.

Browser other questions tagged

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