forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lcompilers#2543 from syheliel/random_fix
refine testcase for random
- Loading branch information
Showing
1 changed file
with
16 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |