-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomSample.py
More file actions
46 lines (32 loc) · 827 Bytes
/
RandomSample.py
File metadata and controls
46 lines (32 loc) · 827 Bytes
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 10 23:52:35 2021
@author: maherme
"""
#%%
# sample does not use replacement
import random
l = list(range(10))
print(random.choices(l, k=5)) # Here you have the risk to have repetitions.
print(random.sample(l, k=5)) # Here you have not repetitons.
#%%
# Let's see an example using a deck simulator:
suits = 'C', 'D', 'H', 'S'
ranks = tuple(range(2, 11)) + tuple('JQKA')
#%%
deck = []
for suit in suits:
for rank in ranks:
deck.append(str(rank) + suit)
print(deck)
#%%
# Or using list comprehension:
deck = [str(rank) + suit for suit in suits for rank in ranks]
print(deck)
#%%
# Notice using sample you don't get repetitions:
from collections import Counter
Counter(random.choices(deck, k=40))
Counter(random.sample(deck, k=52))
#%%