-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex01_random_numbers.py
More file actions
32 lines (25 loc) · 1.04 KB
/
ex01_random_numbers.py
File metadata and controls
32 lines (25 loc) · 1.04 KB
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
28
29
30
31
32
# 01 - the random library
# Make sure to use Python 3, not 2.
# For full documentation, see https://docs.python.org/3/library/random.html
import random
# Set the random seed for the same results each time
random.seed(1)
print("Uniformly distributed random variate between 0 (inclusive) and 1 (exclusive):", random.random())
# sample(seq,k) chooses k samples from seq without replacement
print("Random ordering of 0..5:", random.sample(range(6),6))
# Random coin flip
print("Random coin flip:", random.choice(['heads', 'tails']))
# Random sequence of 10 coin flips
# choices(seq,k) chooses k samples from seq with replacement
print("Random coin flip:", random.choices(['H', 'T'], k=10))
# Exponential distribution
print("Exponential random variates with mean 0.25:",
random.expovariate(4),
random.expovariate(4),
random.expovariate(4),
random.expovariate(4))
print("Exponential random variates with mean 4:",
random.expovariate(1/4),
random.expovariate(1/4),
random.expovariate(1/4),
random.expovariate(1/4))