Feeding Random into itself - does it "corrupt the randomness"?
If I am to reinitialize an instance of System.Random after every .NextDouble() using the return value as a seed, would that make the randomness worse in some way?
I want to do this in order to be able to persist and unpersist a System.Random, via knowing the seed at every step of the way.
45 Replies
Worse? Probably not. Creating a bunch of
Random
instances is more likely to make randomness worse.
Can't you just use a single Random
instance, with a single seed, and reuse that?
I'd maybe generate, like, 100k values using this method of yours and check if they follow normal distributioni understood his footnote like he wants the randomfunction to be deterministic i guess for replayability?
if you create a random on the approximately same time without special seed it will yields the exact same random values as it has teh same seed: the time
You just need one seeded instance for it to be deterministic
this, yeah
the point is to be able to save and resume the randomness, like in a game's save file for instance
Save the seed, then?
I don't think you can extract the seed
you start with a known one
^
yes, but as soon as you random once, the seed is now different
no
No
that's not what a seed is
I misspoke, let me rephrase
You create an instance of
Random
with a given seed. You reuse that instance everywhere
It's guaranteed to be deterministic for the same seedevery time you call next you are now "1 random pull" past the initial seed, therefore even if you use the same seed unless you keep track of how many randoms you pulled you cannot resume back to the same state
so you need to keep track of how many numbers you've generated
hmm I guess that could work, but wouldnt calling Next 1000 times be kind of slow?
1000? probably instantaneous
I mean as time goes on "loading the save" would get slower and slower
or you could look into persisting the
Random
internalsalright, well I might do that then
Save the generated values in the save ¯\_(ツ)_/¯
I did look into it, but it seems they've changed a couple times over the years and I rather not depend on undocumented behavior
not sure what you mean
Instead of regenerating the values from the seed, save them in the savegame
That way you won't have to skip 1000 random results
that doesn't help him
ah, you misunderstand - the idea is to have the next random be the same when you reload
Ah, right
I misunderstood, then
^^
the goal is to persist
Random
so when the game is loaded again it's as if it was never quit at allI'll just keep track of how many randoms were pulled and hope it never becomes a problem xd
thanks for the help ZZZZ and Jimm
personally i'd just assume the implementation of random won't change, the existing changes were made backwards compatible anyway
or implement your own random class that you can control the implementation of
that was going to be my next try 😄
i'm sure there are a pile of PRNG algorithms out there that you can copy and paste
easier to get that past code review than reflection of random underscore properties
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
Benchmarked it, btw:
I got resumable rand class somewhere, let me try to find it
Safe to assume that no, generating a lot of random values won't be an issue
quick calculation even if you generate 60 randoms a second for 50h based on the benchmark above its just around 0.1s to calculate them from new in one batch
This uses SHA256 to hash the state on top of itself You can then persist the 32 byte state and resume from it when required.
Not sure if this is the best way to do this. I don't even know if the
bigInt % int.MaxValue
is safe operation for converting to int..without actually benchmarking it that looks slow
i'd also want to see some proof that this generates a good distribution of random numbers since you're attempting to use SHA256 as a PRNG algorithm
definitely better to just use a proper PRNG implementation in a way that you have access to the internal state to persist
distribution aside, that implementation is 36 times slower than plain
Random
damn thanks for taking the time to benchmark it
i'd just copy the implementation from https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Random.Xoshiro256StarStarImpl.cs,cf33c3a42d4a8ee6 and tweak it to your needs
or see https://prng.di.unimi.it/ for all the generators like it in case a different algorithm is more appropriate