我认为实现这一点的最佳实践是通过双重哈希。要了解更多信息,请阅读此链接:Double Hashing。我将尝试简单地解释它。
您有两个需要预先计算的辅助哈希函数。以及一个for
循环运行的主散列函数。让我们测试一下(这将是伪代码):
key = random_number() //lets get a random number and call it "key"
module = map_size_x // map size for our module
//form of hash function 1 is: h1(key) = key % module, lets compute the hash 1 for our main hash function
aux1 = key % module
//form of hash function 2 is: h2(key) = 1 + (key % module'), where module' is module smaller for a small number (lets use 1), lets compute it:
aux2 = 1 + (key % (module - 1))
//the main hash function which will generate a random permutation is in the form of: h(key, index) = (h1(key) + index*h2(key)) % module. we already have h1 and h2 so lets loop this through:
for (i = 0; i < map_size_x; i++)
{
randomElement = (aux1 + i*aux2) % module //here we have index of the random element
//DO STUFF HERE
}
要获得另一个排列,只需更改 的值key
。有关更多信息,请查看链接。
希望这可以帮助。干杯。