2
私は安全なランダムなハッシュに基づいて適切な配布を得ることができるかどうかを確認しようとしています。私はいつも私の最初のインデックスのために二倍になるようです。私はSecureRandom hash
SecureRandom sr = new SecureRandom();
sr.setSeed(sr.generateSeed(16));
int zero = 0;
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six =0;
for (int i = 0 ; i < 100000; i++) {
long index = sr.nextLong()%6;
if(index == 0)
zero++;
else if (index == 1)
one++;
else if(index == 2)
two++;
else if(index == 3)
three++;
else if(index == 4)
four++;
else if(index == 5)
five++;
}
System.out.println(zero);
System.out.println(one);
System.out.println(two);
System.out.println(three);
System.out.println(four);
System.out.println(five);
System.out.println(six);
あなたはネガを返すことができるSecureRandom.nextLong()
という事実を無視している出力
Here is the output
16548
8362
8314
8175
8272
8210
RNGと%を使用して、バイアスにRNG出力を傾向があります。単に 'sr.nextInt(6)'を使用してください。 – erickson
@erickson、お勧めします。 [詳細情報](http://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator)好奇心のために。 – shmosel
@erickson、ありがとうございます。あなたの提案も機能します。 – Paul