aboutsummaryrefslogtreecommitdiff
path: root/challenge-246/jeanluc2020/python/ch-1.py
blob: 7a96c4e070bf53e8e3a1ade4ad59ade2b499b331 (plain)
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
#!/usr/bin/python3
# https://theweeklychallenge.org/blog/perl-weekly-challenge-246/#TASK1
#
# Task 1: 6 out of 49
# ===================
#
# 6 out of 49 is a German lottery.
#
# Write a script that outputs six unique random integers from the range 1 to
# 49.
#
## Output
##
## 3
## 10
## 11
## 22
## 38
## 49
#
############################################################
##
## discussion
##
############################################################
#
# Collect random numbers until we have 6 of them. In case we randomly select a
# number that we saw already, discard the number and select another random
# number instead.
#
import random

result = {}

def next_rand() -> int:
    res = random.randint(1,49)
    if res in result:
        # collision, calculate a new random number
        return next_rand()
    return res

for i in range(6):
    num = next_rand()
    result[num] = 1

print("\n".join(str(x) for x in sorted(result.keys())))