How to generate 5 digit number automatically?

Asked

Viewed 1,136 times

2

How can I generate a random number with 5 digits that do not repeat in the range [10000 - 99999].

I built the solution low and had generate in a loop the times I ran not returned repeated numbers, but I do not know if it is the best way to do this.

using System;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        private static int GetNumeroConta()
        {
            Random a = new Random(DateTime.Now.Ticks.GetHashCode());
            Thread.Sleep(1);
            return a.Next(10000, 99999);
        }

        static void Main(string[] args)
        {
            int numero = GetNumeroConta();
            Console.WriteLine(numero);

            Console.ReadKey();
        }
    }
}
  • 1

    I think suplicata: https://answall.com/q/17783/101, actually it has several: https://answall.com/search?q=%5Bc%23%5D+Fisher

  • @bigown, can you convert the list of Omo to a number as I described in the question ? I put together a list just to check if it was generating a repeated number.

  • @Bacco, I’m looking for a number, not a list of names.

  • @Marconciliosouza the logic is the same (even in the case of names, you shuffle the "numbers" of each one). Simply you will generate an array or list of numbers of the desired size, and will "shuffle" it to randomize. Positions change, but there’s only gonna be one of each. You can optimize the algorithm in several ways later (not to spend too much memory for nothing if you are going to extract a few numbers, for example), the fundamental is logic. If you want, you can [Edit] the question giving more detailed parameters, then maybe we can give more specific suggestions for your case.

  • Remembering that if editing the question is really different from the questions indicated in the links above, yours can be reopened by the community without problems.

  • @Bacco, I’ll see if I can implement it here, apparently it will work if I close the question later.

  • @Marconciliosouza de qq forma, if you have difficulty or new ideas, leave comment that I or other colleagues can try to help (whether in the suggested solution, or in the issue of the question).

Show 3 more comments

1 answer

1


Implement a "cache" of random numbers, so the created numbers are cached, and you check whether it exists or not:

using System.Collections.Generic;
...

static List<int> random_caches = new List<int>();

private static int GetNumeroConta()
{
    // não é necessário colocar o milissegundo para a semente
    // a semente gerada é com base em Environment.TickCount
    Random a = new Random();
    // para quê isso? é realmente inútil sendo que só irá atrasar em 1ms a semente
    //Thread.Sleep(1);

    // obtemos nosso número aleatório
    int c = a.Next(10000, 99999);
    // verifica se o número está em cache
    while(random_caches.Contains(c)) c = a.Next(10000, 99999);
    // adiciona o número ao cache
    random_caches.Add(c);
    // retorna
    return c;
}

See working on .NET Fiddle.

Browser other questions tagged

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