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();
}
}
}
I think suplicata: https://answall.com/q/17783/101, actually it has several: https://answall.com/search?q=%5Bc%23%5D+Fisher
– Maniero
@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.
– Marco Souza
@Bacco, I’m looking for a number, not a list of names.
– Marco Souza
Possible duplicate of: Draw where the name cannot be drawn more than once
– Jéf Bueno
@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.
– Bacco
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
@Bacco, I’ll see if I can implement it here, apparently it will work if I close the question later.
– Marco Souza
@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).
– Bacco