c# - Class For generating Random Code -
i wrote class generate random code, shouldn't create 2 repetitive number. want know, in code, how possible have collision?
public string myrandom() { random r = new random(); int x = r.next(1000);//max range persiancalendar pc = new persiancalendar(); string _myrandom = pc.getmonth(datetime.now).tostring() + pc.getdayofmonth(datetime.now).tostring() + pc.gethour(datetime.now).tostring() + pc.getminute(datetime.now).tostring() + pc.getsecond(datetime.now).tostring() + pc.getmilliseconds(datetime.now).tostring() + x.tostring(); return _myrandom; }
if do
list<string> s = new list<string>(); for(int = 0; < 100; i++) { s.add(myrandom()); }
why? when random class created in loop, can duplicate random numbers. more info
solution: create random instance outside loop.
Comments
Post a Comment