c# - How to create a true 'Random' with Timer control? -
i have timer clicks button, , no matter speed timer at, due random
having kind of association clock, outcome predictable no matter speed.
i have been reading , learned when new random
initialized, done system time, explains why i'm getting problem.
here's have:
int win = 0; int lose = 0; private void button1_click(object sender, eventargs e) { random rn = new random(); int rnu = rn.next(2); if (rnu == 0) win++; if (rnu == 1) lose++; string winmsg = "wins: " + win.tostring(); string losemsg = "losses: " + lose.tostring(); winlbl.text = winmsg; loselbl.text = losemsg; timer1.start(); }
and timer:
private void timer1_tick(object sender, eventargs e) { button1.performclick(); //interval 500, predictable. }
my goal try create more 'random' outcome- 1 wouldn't predictable timer control. however, can't modify speed of timer; must remain constant cycle.
i've tried stopping timer , restarting, carries on predictable behavior.
does know of way around or kind of 'trick'? or c# random
limitation?
i have been reading , learned when new random initialized, done system time, explains why i'm getting problem.
then move random
outside method , don't reinitialize it.
random rn = new random(); private void button1_click(object sender, eventargs e) {
the point of random
class shouldn't have many instances around program, because initialized @ same time. perfect number 1 each thread (in use it). winforms events run in main thread.
Comments
Post a Comment