By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

Random Number Generation

P: n/a
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,
--
Anil Gupte
www.keeninc.net
www.icinema.com
Oct 21 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Anil Gupte wrote:
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
Oct 21 '07 #2

P: n/a
Anil Gupte wrote:
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
Oct 21 '07 #3

P: n/a
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.

--
Anil Gupte
www.keeninc.net
www.icinema.com

"Arne Vajhřj" <ar**@vajhoej.dkwrote in message
news:47***********************@news.sunsite.dk...
Anil Gupte wrote:
>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

Oct 21 '07 #4

P: n/a
Anil Gupte wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Oct 21 '07 #5

P: n/a
On Sun, 21 Oct 2007 09:38:27 +0530, "Anil Gupte"
<an*******@icinema.comwrote:
>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

Oct 21 '07 #6

P: n/a
Anil Gupte wrote:
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
Oct 21 '07 #7

P: n/a
Thanx!

--
Anil Gupte
www.keeninc.net
www.icinema.com

"rossum" <ro******@coldmail.comwrote in message
news:79********************************@4ax.com...
On Sun, 21 Oct 2007 09:38:27 +0530, "Anil Gupte"
<an*******@icinema.comwrote:
>>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

Oct 21 '07 #8

P: n/a
Thanx!

--
Anil Gupte
www.keeninc.net
www.icinema.com

"Göran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Anil Gupte wrote:
>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.

--
Göran Andersson
_____
http://www.guffa.com

Oct 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.