파이썬 1~100까지 10단위로 끊어서 각 구간마다 확률을 주고 5개를 뽑을려고 하는데요

조회수 882회

random.choices(range(1,100), weights = [1,2,1,1,1])

어떻게 하면 10단위로 끊을수 있을까요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 무식하게 100개짜리 가중치 리스트를 만들어 random.choicesweights 인자로 넘겨보았습니다.

    마지막 부분은 collections.Counter 로 대충 검증해 본 것.

    Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license()" for more information.
    
    >>> 구간확률가중치 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
    >>> 가중치 = [ [i]*10 for i in 구간확률가중치 ]
    >>> 가중치
    [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9, 9, 9, 9, 9], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]]
    >>> 가중치 = sum(가중치, [])
    >>> 가중치
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
    >>> len(가중치)
    100
    >>> import random
    >>> random.choices(range(1, 101), weights=가중치)
    [57]
    >>> random.choices(range(1, 101), weights=가중치)
    [12]
    >>> random.choices(range(1, 101), weights=가중치)
    [82]
    >>> help(random.choices)
    Help on method choices in module random:
    
    choices(population, weights=None, *, cum_weights=None, k=1) method of random.Random instance
        Return a k sized list of population elements chosen with replacement.
    
        If the relative weights or cumulative weights are not specified,
        the selections are made with equal probability.
    
    >>> sample = random.choices(range(1, 101), weights=가중치, k=10000)
    >>> sample
    
    >>> from collections import Counter
    >>> c = Counter(sample)
    >>> for k, v in sorted(c.items()):
        print(k, v)
    
    
    1 17
    2 22
    3 20
    4 21
    5 14
    6 22
    7 21
    8 22
    9 16
    10 32
    11 35
    12 39
    13 26
    14 37
    15 42
    16 31
    17 39
    18 25
    19 42
    20 33
    21 47
    22 52
    23 54
    24 51
    25 66
    26 47
    27 49
    28 55
    29 58
    30 62
    31 78
    32 69
    33 71
    34 54
    35 78
    36 71
    37 69
    38 67
    39 75
    40 73
    41 98
    42 86
    43 96
    44 87
    45 96
    46 92
    47 73
    48 95
    49 98
    50 81
    51 87
    52 93
    53 117
    54 81
    55 105
    56 126
    57 115
    58 101
    59 118
    60 117
    61 138
    62 132
    63 124
    64 141
    65 131
    66 120
    67 131
    68 132
    69 123
    70 137
    71 150
    72 133
    73 153
    74 138
    75 128
    76 155
    77 151
    78 162
    79 155
    80 132
    81 166
    82 179
    83 156
    84 167
    85 168
    86 153
    87 174
    88 198
    89 149
    90 155
    91 189
    92 177
    93 186
    94 176
    95 206
    96 157
    97 177
    98 183
    99 175
    100 179
    >>> 
    
    • 질문있습니다 구간확률가중치를 a라 한다면 a의 10은 1보다 10배 잘 나온다는 말인가요? 알 수 없는 사용자 2020.5.27 23:43
    • 네. 그렇게 이해하고 있습니다. 결과를 보면 그렇게 비슷하게 나왔죠. nowp 2020.5.27 23:49

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)