samedi 15 janvier 2022

Reset Randomize/random number generation seed in VB.NET

So I am creating an application where the user can either input a seed, or a seed is randomly generated for them. Then, this seed is passed to the Randomize function which calls Rnd several times. The intention is that if the same seed is used - either if it's generated randomly, or if it's entered by the user - then the output list of random numbers should be the same.

The problem I ran into is that when I randomly generate the seed, this will throw off the random number generator - and I'm not sure how to reset it entirely. My code looks like this:

If UserEnteredSeed() Then
    Randomize(userSeed)
Else
    Randomize()
    userSeed = Rnd() + Rnd() ' This part is just a placeholder - but the Rnd function is called several times
End If

Randomize(userSeed)

Debug.Print(Rnd().ToString())

My problem is that when the Else clause is reached, the use of the Rnd function will (to my understanding) traverse the list of random numbers, and then when the next Randomize is called with userSeed, the list changes but the position within the list does not. This will change the output when the Else clause is not reached and Rnd is not called several times.

My question is, can I completely reset the random number generation so that regardless of whether the userSeed number is entered or generated, the same userSeed produces the same random number as output? Or if not, how can I effectively create the same intended behaviour?




Aucun commentaire:

Enregistrer un commentaire