❔ Seeing the same Random() seed on separate program startups and different times
Hi all, I'm seeing a non-isolated issue pop up related to the Random() class and its association with the system Environment.TickCount variable. I'm working on .NET Framework 4.5 and while I'm aware that there are issues with Random() and Framework, I'm struggling to see the reason behind my problem.
On program startup a Random object is declared and initialized with the default parameterless constructor. Days, weeks, or even months later, I will generate the same seed on a different program startup (I know this because I print the tick count). This obviously causes collisions with the numbers generated. It's happened enough times that it's causing issues for me, and I feel as if it should be virtually impossible to have this repeatability.
Is there something that I'm fundamentally missing that increases the likelihood of this problem?
private Random mRand = null;
mRand = new Random();
To generate the numbers, I'm calling mRand.Next(65536);
Any help is appreciated, thanks!
14 Replies
Use Random.Shared I guess
Don't think I can use Random.Shared in framework 4.5
create a static property that holds a Random instance
thanks for the response. there's only one Random object that's ever created in the program, and the issue is that I'm getting the same values months later when the program is started up again. would static solve this issue?
the computer and program are both restarted indiscriminately
new Random() should be using the system clock to generate a seed. So unless this code is executing with identical/equivalent time down to 15ms, it's hard to understand how this is happening
But you say you're printing the same tick count?
my thoughts exactly 🙁. I can't even physically open the program fast enough to get the same seed
yes correct
Two thoughts:
Have you validated the assumption that your clocks are going monotonically up?
Why not introduce a real source of randomness (not time)?
When CMOS batteries die computers can see their clocks reset back to the epoch every boot causing problems like that.
in regards to your first question, I've only ever confirmed that the tick count resets on a computer restart and that it increments on successive program startups. also confirmed through [System.Environment]::TickCount in powershell.
I may end up going this route, but perhaps it would be nice to determine a root cause or find some closure
but any modern OS will sync the time on boot
the program itself isn't started on a script or anything, it's all manual so in my mind the tick counts should virtually never sync up like that
Okay I thought ticks were the time but I kept reading and it's time since boot as an i32.
It's not crazy that those would collide if you had a lot of computers but seems very unlikely for one machine and a manually launched program.
I'd say next up is writing a program that logs Environment.TickCount every 100ms or so and let that run for a few days and see if TickCount behaves as expected.
Are you sure you're not doing random= new Random(65536); random.Next();
Somewhere else in code? 😅
perhaps I forgot to mention that it's 5 computers all running the same program. I mean still, it seems extremely unlikely but I have dozens of cases across months
@phaseshift. haha I wish, all these duplicate tickcounts are different values and it's only random() not random(int)
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.