これを達成するためのベストプラクティスは、二重ハッシュを使用することだと思います。詳細については、次のリンクを参照してください: Double Hashing。簡単に説明してみます。
事前に計算する必要がある 2 つの補助ハッシュ関数があります。そして、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
。詳細については、リンクを確認してください。
お役に立てれば。乾杯。