PC Review


Reply
Thread Tools Rate Thread

C# random data

 
 
Maziar Aflatoun
Guest
Posts: n/a
 
      4th Jan 2004
Hi everyone,

I have the following random string generator

public static string getAlphabets(int strLength)
{
string RandomString = "";
Random X = new Random();
for (int x=0; x < strLength; x++) RandomString +=(char)(X.Next(65, 90));
return RandomString;
}

The only problem is that it returnsthe extact same randomly generated string
when called more than one times. Any idea?

Tank you
Maz A.


 
Reply With Quote
 
 
 
 
Dave Quigley
Guest
Posts: n/a
 
      4th Jan 2004
I think random also takes a seed Im not exactly sure what happens when you
dont seed it but it might use the same default one. Try
(int)DateTime.Now.Ticks to seed the generator


"Maziar Aflatoun" <(E-Mail Removed)> wrote in message
news:QBNJb.123684$(E-Mail Removed)...
> Hi everyone,
>
> I have the following random string generator
>
> public static string getAlphabets(int strLength)
> {
> string RandomString = "";
> Random X = new Random();
> for (int x=0; x < strLength; x++) RandomString +=(char)(X.Next(65,

90));
> return RandomString;
> }
>
> The only problem is that it returnsthe extact same randomly generated

string
> when called more than one times. Any idea?
>
> Tank you
> Maz A.
>
>



 
Reply With Quote
 
 
 
 
Richard A. Lowe
Guest
Posts: n/a
 
      4th Jan 2004
You need to move the Random instance outside of your method, into a static
field. Random's constructor, when called, initializes using the time as a
seed value for the pseudo-random generator and so multiple calls may provide
the same tick of time as the seed, causing the first value to be the same.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Maziar Aflatoun" <(E-Mail Removed)> wrote in message
news:QBNJb.123684$(E-Mail Removed)...
> Hi everyone,
>
> I have the following random string generator
>
> public static string getAlphabets(int strLength)
> {
> string RandomString = "";
> Random X = new Random();
> for (int x=0; x < strLength; x++) RandomString +=(char)(X.Next(65,

90));
> return RandomString;
> }
>
> The only problem is that it returnsthe extact same randomly generated

string
> when called more than one times. Any idea?
>
> Tank you
> Maz A.
>
>



 
Reply With Quote
 
John
Guest
Posts: n/a
 
      4th Jan 2004
Or you could simply supply the position of the mouse to the Random
constructor. As long as you accept the assumption that the mouse is
being moved (not good for a service of some sort.) Mathematically
there is extremely low chance the mouse will be in the same position
to times in a row when you create the object.

-john

On Sun, 4 Jan 2004 00:20:01 -0600, "Richard A. Lowe"
<(E-Mail Removed)> wrote:

>You need to move the Random instance outside of your method, into a static
>field. Random's constructor, when called, initializes using the time as a
>seed value for the pseudo-random generator and so multiple calls may provide
>the same tick of time as the seed, causing the first value to be the same.
>
>Richard


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      4th Jan 2004
John <haerolic@-REMOVETHIS-earthlink.net> wrote:
> Or you could simply supply the position of the mouse to the Random
> constructor. As long as you accept the assumption that the mouse is
> being moved (not good for a service of some sort.) Mathematically
> there is extremely low chance the mouse will be in the same position
> to times in a row when you create the object.


That restricts the usage to a GUI though as well. Why make something
which is less random and only works in a certain environment when using
just a single Random instance instead works very well?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Bruno Jouhier [MVP]
Guest
Posts: n/a
 
      4th Jan 2004
Mathematically
> there is extremely low chance the mouse will be in the same position
> to times in a row when you create the object.


No, the chances are not "extremely low". This would be true if the
coordinates were real numbers measured with high precision, but they are
expressed in pixels and so, even if the position of the mouse was really
random, the probability of a collision would be something like 1 / (1024 *
768) > 1e-6, which is not at all "extremely low".

If the machine is a server, it may be running for hours without anybody
moving the mouse. Actually, it may not have its own mouse. In this case, the
chances of getting the same coordinates are very high (probably higher than
getting different coordinates).

IMO, this idea of using the mouse coordinates as seed is just a very bad
idea.

Bruno.



 
Reply With Quote
 
Anthony Borla
Guest
Posts: n/a
 
      4th Jan 2004

"Maziar Aflatoun" <(E-Mail Removed)> wrote in message
news:QBNJb.123684$(E-Mail Removed)...
> Hi everyone,
>
> I have the following random string generator
>
> public static string getAlphabets(int strLength)
> {
> string RandomString = "";
> Random X = new Random();
> for (int x=0; x < strLength; x++)
> RandomString +=(char)(X.Next(65, 90));
> return RandomString;
> }
>
> The only problem is that it returnsthe extact same randomly
> generated string when called more than one times. Any idea?
>


Personally, I've never liked the idea of creating a new 'Random' object
every time you need a pseudo-random value. I'd much rather:

* Create a single, appropriately seeded 'Random' object,
usually as a class field

* Call the relevant method to generate the next
pseudo-random value

The following code [a slightly modified version of your own] follows this
approach, and appears to work correctly.

I hope this helps.

Anthony Borla

// RandomProgram.cs
using System;

public class RandomProgram
{
public static void Main(String[] args)
{
Console.WriteLine(getAlphabets(5));
Console.WriteLine(getAlphabets(3));
Console.WriteLine(getAlphabets(8));
}

public static String getAlphabets(int strLength)
{
String RandomString = "";

for (int x=0; x < strLength; ++x)
RandomString += (char)(X.Next(65, 90));
// RandomString += (X.Next(65, 90)).ToString();

return RandomString;
}

private static Random X = new Random();
}



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: HELP!!! ***res://<random>.dll/<random>.html#<random>*** HELP!!! The Stull Demon Windows XP Security 0 1st Jul 2004 03:55 AM
Re: HELP!!! ***res://<random>.dll/<random>.html#<random>*** HELP!!! The Stull Demon Windows XP Security 13 30th Jun 2004 03:02 PM
Re: HELP!!! ***res://<random>.dll/<random>.html#<random>*** HELP!!! The Stull Demon Windows XP Internet Explorer 11 30th Jun 2004 03:02 PM
HELP!!! ***res://<random>.dll/<random>.html#<random>*** HELP!!! The Stull Demon Windows XP Internet Explorer 1 29th Jun 2004 01:11 AM
HELP!!! ***res://<random>.dll/<random>.html#<random>*** HELP!!! The Stull Demon Windows XP Security 2 28th Jun 2004 10:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:04 PM.