TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Random Number Generation

C#(C Sharp)

 
 
Thread Tools Display Modes
Unread 21-10-2007, 05:01 AM   #1
Anil Gupte
Guest
 
Posts: n/a
Random Number Generation

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



Sponsored Links
 
Unread 21-10-2007, 05:01 AM   #2
Arne Vajh°j
Guest
 
Posts: n/a
Re: Random Number Generation

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
 
Unread 21-10-2007, 05:01 AM   #3
Arne Vajh°j
Guest
 
Posts: n/a
Re: Random Number Generation

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
 
Unread 21-10-2007, 05:59 AM   #4
Anil Gupte
Guest
 
Posts: n/a
Re: Random Number Generation

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" <arne@vajhoej.dk> wrote in message
news:471ad40e$0$90265$14726298@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



 
Unread 21-10-2007, 08:59 AM   #5
G÷ran Andersson
Guest
 
Posts: n/a
Re: Random Number Generation

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
 
Unread 21-10-2007, 10:59 AM   #6
rossum
Guest
 
Posts: n/a
Re: Random Number Generation

On Sun, 21 Oct 2007 09:38:27 +0530, "Anil Gupte"
<anil-list@icinema.com> 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 = "";

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

 
Unread 21-10-2007, 03:03 PM   #7
Arne Vajh°j
Guest
 
Posts: n/a
Re: Random Number Generation

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
 
Unread 21-10-2007, 05:02 PM   #8
Anil Gupte
Guest
 
Posts: n/a
Re: Random Number Generation

Thanx!

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

"rossum" <rossum48@coldmail.com> wrote in message
news:799mh312ctkv1jiju6f0efjbgugeeo7h7v@4ax.com...
> On Sun, 21 Oct 2007 09:38:27 +0530, "Anil Gupte"
> <anil-list@icinema.com> 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 = "";

> 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
>



 
Unread 21-10-2007, 05:02 PM   #9
Anil Gupte
Guest
 
Posts: n/a
Re: Random Number Generation

Sponsored Links
Thanx!

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

"G÷ran Andersson" <guffa@guffa.com> wrote in message
news:%23UFCE97EIHA.5328@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




Sponsored Links
 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 10:05 PM.


vBulletin, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © 2005-2016, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional