1

I have an application which is storing the requests.

I am getting Multiple request in timespan of miliseconds.

I am create a Unique ID like

Random _r = new Random();
int n = _r.Next(9);
String.Format("{0:yyyyMMddHHmmss}{1}", DateTime.Now, n.ToString());

But when the multiple request are coming on timespan of miliseconds. This UniqueID is getting repeated.

I am storing those requests with one unique id. but its getting repeated if request are coming on timespan of miliseconds

Please help me on this....If i am wrong anywhere please suggest me somewhere..

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ashu Rawat
  • 21
  • 1

4 Answers4

2

You need 1 instance of Random that is referenced from each execution of your routine.

public class Helper
{
    Random _r = new Random();
    public string GetUniqueId()
    {
        int n = _r.Next(9);
        return String.Format("{0:yyyyMMddHHmmss}{1}", DateTime.Now, n.ToString());
    }
}

You're running into the issue that occurs when you instantiate many Randoms within a small interval. Each instance ends up with the same seed value so all their pseudo-random series of values will be identical. Using 1 instance for all calls guarantees the next value in the series.

Note, the likelihood of still getting the same value in a row is inversely proportional to the size of the maxValue argument of Next.

Sorax
  • 2,205
  • 13
  • 14
  • Sorry, Please elaborate this..i am not getting fully.. – Ashu Rawat Oct 07 '13 at 15:42
  • @user2833537 When you instantiate Random, it uses the current date and time as the seed. When you instantiate Random many times in quick succession, the seed has no time to change. Additionally, each time you instantiate Random, it starts over from the first number. This is what causes you to get the same number many times in a row. – MildWolfie Oct 07 '13 at 16:55
  • This will not, however, guarantee uniqueness. It is quite possible (and likely given the small range) that a returned number from 0 - 8 will be returned. – Chris Dunaway Oct 07 '13 at 19:37
  • True. I should use different wording. I'll update the answer. – Sorax Oct 07 '13 at 19:54
  • Why use `.Next(9)` in the first place. The nine values are not particularly many. You could use `.Next(10000)` or something instead. If the three extra digits is a problem, drop the leading three digits of the year (unless it is important to be able to recover the year, of course). Can use format element `{1:D4}` to make sure the random number is always four digits long. – Jeppe Stig Nielsen Oct 07 '13 at 20:19
  • Please note that the reason why the same seed is used if the Random object is created within milliseconds of each other is because it is seeded by the system clock, which will return the same value if your code is fast enough. http://msdn.microsoft.com/en-us/library/h343ddh9.aspx – Jun Wei Lee Oct 07 '13 at 23:19
0

Yo should define and create _r in a higher scope than the one which runs your method. Than, your method should use this instance of _r, to get other random generated numbers.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
0

This will generate a unique ID for your case -

Random _r = new Random();
int n = _r.Next(9);
String.Format("{0}{1}", DateTime.Now.Ticks, n.ToString());

OR for millisecond precision , you can use

Random _r = new Random();
int n = _r.Next(9);
String.Format("{0:yyyyMMddHHmmssfff}{1}", DateTime.Now, n.ToString());
Vandesh
  • 6,368
  • 1
  • 26
  • 38
0

Every time you run your code, you create a new Random object. The thing about the Random object is that it isn't really random. If you seed it with the same value, it will give you the same number. Therefore, if you create two Random objects at the same time, they'll produce the same value, because the Random() constructor with no arguments uses the current time as a seed. The current time is not that accurate, so all Random() objects created within one small span of time (around 15 milliseconds) will generate the same sequence of numbers because the time has not changed.

Here's an example of how you can fix it - create your Random object just once:

public class MyApp
{
    private Random _r;
    public MyApp()
    {
        this._r = new Random();
    }

    public handle_request()
    {
        int n = _r.Next(9);
        String.Format("{0:yyyyMMddHHmmss}{1}", DateTime.Now, n.ToString());
    }
}
Kevin T
  • 216
  • 1
  • 6