views:

162

answers:

6

I want to generate 25 unique random numbers and list them in a console. The numbers should be atleast 10 characters long. Any easy way to do that?

A: 
Random rand = new Random();
for (int i = 0; i < 25; i++)
    Console.WriteLine((ulong)(rand.NextDouble()*10000000000) + 1000000000);

For the uniqueness just add a test if number was generated before.

Itay
step in the right direction, now "The numbers should be atleast 10 characters long"
PoweRoy
Not guaranteed to be unique.
crazyscot
@crazyscot: No, but thats pretty likely, taking 25 out of ~10^7.
Jens
+2  A: 

The problem lies a little in "25 unique random". Displaying 25 random numbers is as easy as

Random r = new Random();
for(int i=0; i<25; i++)
    Console.WriteLine(r.Next(1,100).ToString());

These are not necessarily unique, though. If you do not want to allow duplicates, you need to store previously generated numbers somehow, and roll again if you hit an old one.

Be aware that you change the probability distribution of your generated numbers this way.

Edit: I've just noticed that these numbers should be ten characters long. Since 9,999,999,999 exceeds Int32.MaxValue, I'd suggest using Math.Floor(r.NextDouble() * 10000000000 + 1000000000) instead of r.Next(1,100).

Since your numbers are that long, you should not need to worry about duplicates. They are very very unlikely.

Jens
`999 999 999` does not exceed `int.MaxValue`. Multiplying `r.Next()` by a billion might however.
IVlad
@IVlad: I meant to write 10 '9's. `r.Next()` returns a double instead of an int, that why I used it.
Jens
@Jens - no, `r.Next()` returns an int. You want `r.NextDouble()`, and even then you are overcomplicating things.
IVlad
A: 
 Random rnd = new Random(table);
 for(int i = 0; i < 25; ++i) {
   Console.WriteLine("{0}", rnd.Next(50, 50+i) 
 }
Preet Sangha
Not guaranteed to be unique.
crazyscot
Can you suggest a technique on a digital computer that is guaranteed. And I mean 100%?
Preet Sangha
+7  A: 

Try building the numbers up as strings, and use a HashSet to ensure they are unique:

Random random = new Random();
HashSet<string> ids = new HashSet<string>();

while (ids.Count < 25)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10; ++i)
    {
        sb.Append(random.Next(10));
    }
    ids.Add(sb.ToString());
}

Example output:

7895499338
2643703497
0126762624
8623017810
...etc...

The class HashSet is present in .NET 3.5 and newer.

Mark Byers
A: 

One simple way is this:

class Test
{
    private static void Main()
    {
        Random rand = new Random();

        for (int i = 0; i < 25; ++i)
        {
            Console.WriteLine(rand.Next(1000000000, int.MaxValue));
        }
    }
}

This will ensure that the numbers are always 10 characters (digits) long. They will not necessarily be unique however. If you want them to definitely be unique, you'll have to do something like this:

class Test
{
    private static void Main()
    {
        Random rand = new Random();

        var generatedSoFar = new HashSet<int>();
        for (int i = 0; i < 25; ++i)
        {
            int newRand;
            do
            {
                newRand = rand.Next(1000000000, int.MaxValue);
            } while (generatedSoFar.Contains(newRand)); // generate a new random number until we get to one we haven't generated before

            generatedSoFar.Add(newRand);

            Console.WriteLine(newRand);
        }
    }
}

If you want to be able to have more than ten digits, you generate the number of digits randomly between 10 and your max number of digits. Then generate each digit (or group of digits) randomly in a StringBuilder or List. You can use the same HashSet method I used above to ensure uniqueness.

IVlad
A: 

There is a big different between Randomness and Uniqueness.

So if you need really unique numbers you have to make sure that you save somewhere all already created numbers and check if your newly created one isn't within this list or you have to provide some algorithm that ensures that a given number can't created twice.

To get the second part to work you mostly take the date/time of the creation moment, cause the current date/time pair is unique forever. The only problem is how many creations per (milli)second do you have and how many digits are available to store your unique number.

A sample about using 12 digits is made here. Hope this helps.

Oliver