lundi 2 octobre 2023

C-Sharp -- Generating random integers from nonrandom seeds

I'm making a game, and every now and then a number of objects in the game need to know whether or not the player has moved. Because of the large numbers of objects involved, instead of alerting every object every frame, I've sorted them into bins with corresponding events. Bin 1 is alerted the first frame the player moves, then bin 2 the second frame, etc, up to some finite number.

However, I need some consistent way to assign each object a bin. Just using the object ids isn't working, as the way that the objects are generated gives patterns in their ids that may result in some bins being filled more or less than others, which decreases the gains in efficiency. If all the ids are even and so is the bin size, then only the even bins will be filled.

In short, the problem is this:

Given a sequence of non-random, unknown, unique integers S that may or may not have a pattern, how can I choose a hashing algorithm H and number of bins p so that H(S[i]) % p forms a roughly uniform distribution?

Simply choosing a prime number as the number of bins seems to be a good start, but I'm wondering if there's a hashing algorithm I can use to "scramble" the patterns of the input sequence and improve uniformity. Ideally, one already included in .Net.

Edit:

I've found some promising results in this answer. I'm still curious about this problem though, both from a mathematical perspective and whether there are tools to do this included in .NET.




Aucun commentaire:

Enregistrer un commentaire