-
Notifications
You must be signed in to change notification settings - Fork 0
/
S0670_random.py
41 lines (31 loc) · 1.32 KB
/
S0670_random.py
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
33
34
35
36
37
38
39
40
41
"""
Random numbers:
Inspicer følgende kode i detaljer. Find ud af, hvad hver række kode gør.
F.eks. ved at ændre koden en smule og derefter køre/debugge programmet.
Derefter går du videre med den næste fil."""
import random # this imports a library called "random". A library is (someone else's) python code, that you can use in your own program.
for i in range(3):
print(f"A random number between 0 and 1: {random.random()}")
print()
minimum = 2
maximum = 4
for i in range(5):
print(f"A random integer number between {minimum} and {maximum}: {random.randint(minimum, maximum)}") #
print()
first_seed = 5
random.seed(first_seed) # use seed() to create reproducible random numbers
for i in range(3):
print(f"A random number between 0 and 1 with seed {first_seed}: {random.random()}")
print()
second_seed = 7 # another seed
random.seed(second_seed)
for i in range(3):
print(f"A random number between 0 and 1 with seed {second_seed}: {random.random()}")
print()
random.seed(first_seed) # same seed from before
for i in range(3):
print(f"A random number between 0 and 1 with seed {first_seed}: {random.random()}") # using the first seed again still creates the same random numbers
print()
max_number = 8
for i in range(3):
print(f"A random number between 0 and {max_number}: {random.random()*max_number}")