-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom module.py
More file actions
65 lines (43 loc) · 2.13 KB
/
Copy pathrandom module.py
File metadata and controls
65 lines (43 loc) · 2.13 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
## Random Module
# it is used to generate random numbers and make random selections.
# this are enough to learn random.random() 1.random.random(), 2.random.randint(), 3.random.choice(), 4.random.shuffle()
# all are this 1.random.random(), 2.random.randint(a, b), 3.random.randrange(start, stop, step), 4.random.uniform(a, b), 5.random.choice(sequence), 6.random.shuffle(list), 7.random.sample(sequence, k)
### 1.random.random()
import random
print(random.random( ))
### 2.random.randint(a, b)
import random
print(random.randint(1,10)) #Returns a random integer between **a and b** (inclusive).
#------for practice------
import random
dice=random.randint(1,6)
print("dice value:",dice)
### 3.random.randrange(start, stop, step)
import random
print(random.randrange(1, 10, 2)) #Returns a random number from a range
### 4.random.uniform(a, b)
import random
print(random.uniform(1, 10)) #Returns a random floating-point number between a and b.
### 5.random.choice(sequence)
import random
fruits = ["apple", "banana", "mango"]
print(random.choice(fruits)) #Returns a random item from a list, tuple, or string.
### 6.random.shuffle(list)
import random
nums = [1, 2, 3, 4, 5]
random.shuffle(nums) #Shuffles a list in place.
print(nums)
### 7.random.sample(sequence, k)
import random
nums = [1, 2, 3, 4, 5]
print(random.sample(nums, 2)) #Returns `k` unique random items.
## Interview Definitions
#| Function | Definition |
#| --------------------- | ---------------------------------------------- |
#| `random.random()` | Returns a random float between 0.0 and 1.0 |
#| `random.randint(a,b)` | Returns a random integer between a and b |
#| `random.randrange()` | Returns a random number from a specified range |
#| `random.uniform(a,b)` | Returns a random float between a and b |
#| `random.choice()` | Returns a random element from a sequence |
#| `random.shuffle()` | Randomly rearranges the elements of a list |
#| `random.sample()` | Returns unique random elements from a sequence |