forked from momo1106github/CNS_Final_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleRoundFlipCoin.py
35 lines (26 loc) · 1009 Bytes
/
SingleRoundFlipCoin.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
import random
class SingleRoundFlipCoin():
"""
This class implement some methods for single round flip coin.
"""
def __init__(self):
"""
The constructor for SingleRoundFlipCoin class.
"""
self.HEAD = 0
self.TAIL = 1
def get_rand_res(self, face = 0, prob = 0.5):
"""
The function to get randomized response.
Parameters:
face (int): Coin face (Head or Tail) that is thrown by user.
prob (float): Probability (range from 0 to 1) of showing user's real answer.
Returns:
Randomized response(0 for Head, 1 for Tail).
"""
first_toss = random.choices([self.HEAD, self.TAIL], weights = [prob, 1 - prob], k = 1)[0]
if (first_toss == self.HEAD):
return int(not(not(face)))
second_toss = random.choices(population = [self.HEAD, self.TAIL], weights = [0.5, 0.5], k = 1)[0]
return int(not(not(second_toss)))
help(SingleRoundFlipCoin)