File size: 767 Bytes
2b534de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
def print_randomly(a ,p=1):
    """
    p is the probability of printing.
    """
    if p<1:
        if random.random()>=p:
            return
    print(a)


__printed_values = {}
def print_once( a, id_ ):
    if id_ not in __printed_values:
        print(a)
        __printed_values[id_] = True


__printed_count = {}
def print_randomly_with_limit(
    a,
    id_,
    p=1, 
    MAX_prints=5,
):
    """
    p: the probability of printing
    MAX_prints: the maximum number of times to allow printing of 'a'
    """
    if id_ not in __printed_count:
        __printed_count[id_] = 0
    if __printed_count[id_] >= MAX_prints:
        return
    if p < 1:
        if random.random() >= p:
            return
    print(a)
    __printed_count[id_] += 1