In my c# code I have a static method. Here is the code sample:
public class RandomKey
{
public static string GetKey()
{
Random rng = new Ran
You keep reinitializing the Random value. Move that out to a static field. Also you can format numbers in hex using ToString
with a formatter.
Also, DateTime.Now
is a bad idea. See this answer for a better way to allocate unique, timestamp values.
The reason is that you are initializing the Random
object inside the method.
When you call the method in close time proximity (like inside a loop), the Random
object is initialized with the same seed. (see Matthew Watson's comment to find out why.)
To prevent that you should declare and initialize the Random
object as a static field, like this:
public class RandomKey
{
static Random rng = new Random();
public static string GetKey()
{
// do your stuff...
}
}