I tested the Math.random() method in Java, but the result didn't show up in the [0.001, 0.01) range, i wonder if this is accidental or algorithmic. Here is the code I tested:
public class TestMathRandom {
public static void main(String[] args) {
for (int i = 0; i < 100; i++)
testRandom();
}
public static void testRandom() {
int _1zero = 0;
int _2zero = 0;
int _3zero = 0;
int _4zero = 0;
int _nulZero = 0;
for (int i = 0; i < 1000_000; i++) {
double d = Math.random();
/*System.out.print(d + "\t");
if (i % 50 == 0)
System.out.println();*/
if (d * 10000 < 1.0)
_4zero++;
else if (d * 1000 < 1.0)
_3zero++;
else if (d * 100 < 1.0)
_3zero++;
else if (d * 10 < 1.0)
_1zero++;
else
_nulZero++;
}
System.out.println();
System.out.println("(0, 0.0001): " + _4zero);
System.out.println("[0.0001, 0.001): " + _3zero);
System.out.println("[0.001, 0.01): " + _2zero);
System.out.println("[0.01, 0.1): " + _1zero);
System.out.println("[0.1, 1): " + _nulZero);
System.out.println();
}
}
The program execution result is:
(0, 0.0001): 102 [0.0001, 0.001): 9923 [0.001, 0.01): 0 [0.01, 0.1): 90042 [0.1, 1): 899933
(0, 0.0001): 119 [0.0001, 0.001): 9963 [0.001, 0.01): 0 [0.01, 0.1): 90118 [0.1, 1): 899800
(0, 0.0001): 95 [0.0001, 0.001): 9871 [0.001, 0.01): 0 [0.01, 0.1): 89991 [0.1, 1): 900043
(0, 0.0001): 97 [0.0001, 0.001): 9958 [0.001, 0.01): 0 [0.01, 0.1): 90120 [0.1, 1): 899825
(0, 0.0001): 117 [0.0001, 0.001): 9872 [0.001, 0.01): 0 [0.01, 0.1): 89763 [0.1, 1): 900248
(0, 0.0001): 97 [0.0001, 0.001): 9695 [0.001, 0.01): 0 [0.01, 0.1): 89852 [0.1, 1): 900356
(0, 0.0001): 105 [0.0001, 0.001): 9812 [0.001, 0.01): 0 [0.01, 0.1): 90067 [0.1, 1): 900016
(0, 0.0001): 101 [0.0001, 0.001): 9922 [0.001, 0.01): 0 [0.01, 0.1): 89769 [0.1, 1): 900208
(0, 0.0001): 100 [0.0001, 0.001): 9972 [0.001, 0.01): 0 [0.01, 0.1): 89918 [0.1, 1): 900010
(0, 0.0001): 97 [0.0001, 0.001): 9987 [0.001, 0.01): 0 [0.01, 0.1): 90384 [0.1, 1): 899532
And my execution environment is:
Runtime version: 11.0.5+10-b520.30 x86_64 VM: OpenJDK 64-Bit Server VM macOS 10.15.5 GC: ParNew, ConcurrentMarkSweep Memory: 1981M Cores: 8
Aucun commentaire:
Enregistrer un commentaire