File: test_random.py

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (27 lines) | stat: -rw-r--r-- 560 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# test the random module

import random

result = ('FAIL', 'PASS')

# test getrandbits
hist = 8 * [0]
for i in range(300):
    r = random.getrandbits(3)
    hist[r] += 1
ok = all(h >= 10 for h in hist)
print('getrandbits:', result[ok])

# test seed
random.seed(0x1234567)
r1 = random.getrandbits(10)
random.seed(0x1234567)
r2 = random.getrandbits(10)
print('seed:       ', result[r1 == r2])

# test randint is within the given range
ok = True
for i in range(100):
    if not 0 <= random.randint(0, 9) <= 9:
        ok = False
print('randint:    ', result[ok])