c# - Random Number is getting Repeated in case when Multiple Request Coming in Miliseconds -


this question has answer here:

i have application storing requests.

i getting multiple request in timespan of miliseconds.

i create unique id like

random _r = new random(); int n = _r.next(9); string.format("{0:yyyymmddhhmmss}{1}", datetime.now, n.tostring()); 

but when multiple request coming on timespan of miliseconds. uniqueid getting repeated.

i storing requests 1 unique id. getting repeated if request coming on timespan of miliseconds

please me on this....if wrong anywhere please suggest me somewhere..

you need 1 instance of random referenced each execution of 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 issue occurs when instantiate many randoms within small interval. each instance ends same seed value pseudo-random series of values identical. using 1 instance calls guarantees next value in series.

note, likelihood of still getting same value in row inversely proportional size of maxvalue argument of next.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -