Skip to content

refine testcase for random #2543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions integration_tests/test_random_02.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
from lpython import f64
from lpython import i32, f64
import random

def test_seed():
random.seed()
t1: f64 = random.random()
t2: f64 = random.random()
print(t1, t2)
assert abs(t1 - t2) > 1e-3
"""test the distribution of random"""
num_samples:i32 = 100000
bins: list[i32] = [0]*10
_ : i32
for _ in range(num_samples):
val: f64 = random.random()
assert val >= 0.0 and val < 1.0 # value out of range
bins[i32(val * 10.0)] += 1 # increment the appropriate bin

# Check that no bin has significantly more or fewer values than expected
expected_bin_count:i32 = i32(num_samples / 10)
count : i32
for count in bins:
blas: f64 = f64(abs(count - expected_bin_count))
assert blas < f64(expected_bin_count) * 0.05 # allow 5% deviation

test_seed()