-2

I am making a C# script in visual studio for windows forms.
I need to get non duplicated numbers and that is what I have tried in that script, what happens is that when i compile the project, I get this random numbers, but sometimes, those numbers are duplicated, so I would like if you tell me which mistake I have made, or even if the mistake is in that piece of code.
I need to store this random numbers from 1 to 75 to say if these 75 created random numbers, compared with a table with some numbers that I already have are equal.

        for (int i = 0; i < 76; i++)
        {
            auxiliar = random.Next(1, 75);
            bool continuar = false;

            while (!continuar)
            {
                for (int j = 0; j <= contador; j++)
                {
                    if (auxiliar.ToString() == totalBalotas[j])
                    {
                        continuar = true;
                        j = contador;
                    }
                }
                if (continuar)
                {
                    auxiliar = random.Next(1, 75);
                    devuelve = auxiliar.ToString();
                    continuar = false;
                    estaContando = true;
                    return devuelve;
                }
                else
                {
                    continuar = true;
                    totalBalotas[contador] = auxiliar.ToString();
                    devuelve = auxiliar.ToString();
                    contador++;
                    estaContando = true;
                    return devuelve;
                }
            }
        }
        estaContando = true;
        return devuelve;
    }

This is the code.
https://i.stack.imgur.com/D7wqk.png

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
  • The script is on here https://i.stack.imgur.com/RcxIl.png – Nicolas Morales Escobar May 23 '18 at 16:48
  • 3
    Welcome to Stack Overflow. Please don't forget to include your code in your question. You can [edit] the question. We strongly discourage showing code or data in images. Cut and paste **the text** of your code into the question, and indent it by four spaces. – O. Jones May 23 '18 at 16:48
  • 2
    It can't be a random number if duplicates are not allowed. Google "c# random shuffle" to get ahead. – Hans Passant May 23 '18 at 16:51
  • 2
    Please paste your code into the question *as text*. –  May 23 '18 at 16:56
  • 2
    That code makes no sense to me. Post code not an image. – paparazzo May 23 '18 at 16:56
  • "I am making a C# script" ... so show us . BTW C# is not script – Jacek Cz May 23 '18 at 16:59
  • I've removed your visual studio tag because this question isn't related to visual studio (the development environment) – ProgrammingLlama May 23 '18 at 17:02
  • @JacekCz It depends on context. Unity tends to refer to C# files as "scripts" in its documentation, so many Unity developers tend to use the same terminology. What's important is that we understood what the OP meant by "script". –  May 23 '18 at 17:06
  • 1
    Why are you generating 76 numbers from 1 to 75? Are you trying to rearrange the numbers from 1 to 75 in a random order? That is a shuffle, not random. – Dour High Arch May 23 '18 at 17:07
  • Build a list with your numbers 1-75 and use [this](https://stackoverflow.com/questions/273313/randomize-a-listt) to shuffle it. – ProgrammingLlama May 23 '18 at 17:21

2 Answers2

-1

Could you say what is the purpose for generating non duplicate numbers? Should these numbers be random? Are there any boundaries on these numbers?

What I can think of right now is using Random to generate numbers and store already used numbers in a collection to avoid duplicates.

Here is example of class that provides unique random numbers. You can think about making it static and threadsafe if you have need for it.

class NonDuplicateRandomNumbersGenerator
{
    private Random r = new Random();
    private ISet<int> usedNumbers = new HashSet<int>();

    public int NonDuplicateRandomNumber()
    {
        var number = r.Next();

        while (usedNumbers.Contains(number))
        {
            number = r.Next();
        }

        usedNumbers.Add(number);
        return number;
    }
}
  • And simply generating the numbers 1-75 and shuffling them isn't a better option? – ProgrammingLlama May 23 '18 at 17:16
  • Hi, look... I need to make a Bingo, I need to store this random numbers from 1 to 75 to say if these 75 created random numbers, compared with a table with some numbers that I already have are equal... Thanks for the answer... – Nicolas Morales Escobar May 23 '18 at 17:22
  • @Nicolas you should have told us that to start with. This answer will not do what you want; you want a shuffle as explained in john's comment. – Dour High Arch May 23 '18 at 19:08
-2

i can understand a little what you are trying to explain, when generating random numbers these can be repeated if you want to generate non-repeated randoms you will have to compare with the whole list of numbers already generated by each generated number.