C# random data

  • Thread starter Thread starter Maziar Aflatoun
  • Start date Start date
M

Maziar Aflatoun

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.
 
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
 
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
 
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
 
John said:
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?
 
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.
 
Maziar Aflatoun said:
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();
}
 
Back
Top