What I want to do:
Based on the contents of a string (like a1b2c3d4e5
, for example), I want to generate a bunch of "repeatable" random events. Generating repeatable random numbers in Golang is easy - you just seed the random number generator with a specific number using the rand.Seed() function, and then you are done.
However, the rand.Seed() function takes an int64 as an argument. So I will need to convert a string to a int64 somehow.
What I tried already:
My first thought was to encode the string using base64, and then convert the sequence of bytes to a int64. However, through some basic testing that only seems to support strings of around 7 length. After I add an 8th character, the number just stays the same.
I guess the fundamental problem here is that there are more possible string values than there are possible int64 values.
So what about some kind of hash function? Most hash functions that I know of return a sequence of bytes; I would need some kind of hash function that returns an int64. Perhaps this approach is also misguided?
I'm stuck.
Aucun commentaire:
Enregistrer un commentaire