Edit: so basically what I'm trying to write is a 1 bit hash for double
.
I want to map a double
to true
or false
with 50/50 chance. For that I wrote code that picks some random numbers (just as an example, I want to use this on data with regularities and still get 50/50 result), checks their last bit and increments y
if it is 1, or n
if it is 0. However, this code constantly results in 25% y
and 75% n
. Why is it not 50/50? And why such a weird, but straight-forward (1/3) distribution?
public class DoubleToBoolean {
@Test
public void test() {
int y = 0;
int n = 0;
Random r = new Random();
for (int i = 0; i < 1000000; i++) {
double randomValue = r.nextDouble();
long lastBit = Double.doubleToLongBits(randomValue) & 1;
if (lastBit == 1) {
y++;
} else {
n++;
}
}
System.out.println(y + " " + n);
}
}
Example output:
250167 749833
Aucun commentaire:
Enregistrer un commentaire