Random Number Generation

A

Anil Gupte

I had someone write a random number generator in C# (I am more of a VB
programmer) and they came up with the following:

public string GetRand(int count)
{
string number = "";
for (int i=0; i<count; i++)
{
Random Rnd = new Random();
number = number+Convert.ToString(Rnd.Next(0,9));
}
return number;
}

I want to return a string of digits where each each string is unique.
Unfrotunately, this function is retrning strings like "11111111" and
"44444444" and so on. This means the seed is not getting initialized on
each interation but only on the first one. Now in VB, I would have put the
Randomize() satement inside the loop so a new seed is innitialized. Is
there an equivalent in C#? Is there a better way to do what we need?

Thanx,
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Anil said:
I had someone write a random number generator in C# (I am more of a VB
programmer) and they came up with the following:

public string GetRand(int count)
{
string number = "";
for (int i=0; i<count; i++)
{
Random Rnd = new Random();
number = number+Convert.ToString(Rnd.Next(0,9));
}
return number;
}

I want to return a string of digits where each each string is unique.
Unfrotunately, this function is retrning strings like "11111111" and
"44444444" and so on. This means the seed is not getting initialized on
each interation but only on the first one. Now in VB, I would have put the
Randomize() satement inside the loop so a new seed is innitialized. Is
there an equivalent in C#? Is there a better way to do what we need?

Try:

private static Random Rnd = new Random();
public string GetRand(int count)
{
string number = "";
for (int i=0; i<count; i++)
{
number = number+Convert.ToString(Rnd.Next(0,9));
}
return number;
}

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Anil said:
I had someone write a random number generator in C# (I am more of a VB
programmer) and they came up with the following:

public string GetRand(int count)
{
string number = "";
for (int i=0; i<count; i++)
{
Random Rnd = new Random();
number = number+Convert.ToString(Rnd.Next(0,9));
}
return number;
}

I want to return a string of digits where each each string is unique.
Unfrotunately, this function is retrning strings like "11111111" and
"44444444" and so on. This means the seed is not getting initialized on
each interation but only on the first one. Now in VB, I would have put the
Randomize() satement inside the loop so a new seed is innitialized. Is
there an equivalent in C#? Is there a better way to do what we need?

BTW, random strings are not unique !

Arne
 
A

Anil Gupte

I thought Random strings should be unique. I understand that this is a
pseudo random number being generated, but as I understand it, the seed is
generated from the system clock and so there is at least a good chance that
they are unique. In fact, I realize that even in a perfect random number
generator there is a chance that the same number will keep appearing, but
the probability of that is fairly low.

Anyway, thanx for that function in your previous message, I will try it.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Anil said:
I thought Random strings should be unique.

No, the strings are not guaranteed to be unique. If they were, they
would not be random. Each random number has the same probability for any
number in the range, it won't have lower probability for numbers that
has been used before.

If you mean that each random values has an even probability in the
range, that's true, but that is not uniqueness.
I understand that this is a
pseudo random number being generated, but as I understand it, the seed is
generated from the system clock and so there is at least a good chance that
they are unique. In fact, I realize that even in a perfect random number
generator there is a chance that the same number will keep appearing, but
the probability of that is fairly low.

The Random object is only seeded from the clock when it's created. The
subsequent random numbers are seeded from the previous random number.

If you create a new Random object for every random number you pick, it
will be seeded from the system clock every time. If you do that too
close in time, like in the loop of your method, the system clock haven't
changed, and you get the same random number over and over.
 
R

rossum

I had someone write a random number generator in C# (I am more of a VB
programmer) and they came up with the following:

public string GetRand(int count)
{
string number = "";
Better to use StringBuilder and append.
for (int i=0; i<count; i++)
{
Random Rnd = new Random();
See comments below.
number = number+Convert.ToString(Rnd.Next(0,9));
Rnd.Next(10) has the same effect.
}
return number;
}

I want to return a string of digits where each each string is unique.
The code you have does nothing to make sure that the string produced
is unique. One way to avoid repeated strings is to use a different
seed for each string, with sufficiently long strings there is a
one-to-one relationship between strings and seeds.
Unfrotunately, this function is retrning strings like "11111111" and
"44444444" and so on.
Using new Random() initialises the RNG from the clock. Putting this
in a tight loop means that the clock does not have time to tick over
between calls, so all the digits generated are repeats. THe code need
to move the new Random() outside the loop.
This means the seed is not getting initialized on
each interation but only on the first one.
It is initialised on each loop, but because the clock has not changed
it is being initialised to the same value and giving the same output.
Now in VB, I would have put the
Randomize() satement inside the loop so a new seed is innitialized. Is
there an equivalent in C#? Is there a better way to do what we need?

Thanx,

Incorporating this into code, and using StringBuilder rather than
creating a lot of immutable strings, I would write:

public string GetRand(int seed, int count) {
Random rand = new Random(seed);
StringBuilder sb = new StringBuilder(count);
for (int i = 0; i < count; ++i) {
sb.Append((char)('0' + rand.Next(10)));
}
return sb.ToString();
}

For long enough strings, each seed will give a unique string. If you
never repeat a seed then you will never repeat a string.

rossum
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Anil said:
I thought Random strings should be unique. I understand that this is a
pseudo random number being generated, but as I understand it, the seed is
generated from the system clock and so there is at least a good chance that
they are unique. In fact, I realize that even in a perfect random number
generator there is a chance that the same number will keep appearing, but
the probability of that is fairly low.

If you generate 2 numbers with 5 digits this way the probability
is 1/10000 that there will be a duplicate.

If you generate 100 numbers with 5 digits this way the probability
is about 40% that there will be a duplicate.

If you google for uuid or guid you will find better approaches
to ensure uniqueness (even though some of them include random
generators).

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top