Skip to content

Commit 0825a70

Browse files
committed
refine testcase for random
1 parent 960fdda commit 0825a70

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
from lpython import f64
1+
from lpython import i32, f64
22
import random
33

44
def test_seed():
5-
random.seed()
6-
t1: f64 = random.random()
7-
t2: f64 = random.random()
8-
print(t1, t2)
9-
assert abs(t1 - t2) > 1e-3
5+
"""test the distribution of random"""
6+
num_samples:i32 = 100000
7+
bins: list[i32] = [0]*10
8+
_ : i32
9+
for _ in range(num_samples):
10+
val: f64 = random.random()
11+
assert val >= 0.0 and val < 1.0 # value out of range
12+
bins[i32(val * 10.0)] += 1 # increment the appropriate bin
13+
14+
# Check that no bin has significantly more or fewer values than expected
15+
expected_bin_count:i32 = i32(num_samples / 10)
16+
count : i32
17+
for count in bins:
18+
blas: f64 = f64(abs(count - expected_bin_count))
19+
assert blas < f64(expected_bin_count) * 0.05 # allow 5% deviation
1020

1121
test_seed()

0 commit comments

Comments
 (0)