From Python to 100, we're going to divide it into 10 units and give each section a chance to pick 5

Asked 2 years ago, Updated 2 years ago, 21 views

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

How can I cut it off in 10 units?

python

2022-09-20 22:16

1 Answers

We made an ignorant list of 100 weights and passed it to the weights factor of random.choice.

The last part was roughly verified with 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.

>>> Interval Probability Weight = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> Weight = [i]*10 for i in interval probability weight ]
>>> Weight
[[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]]
>>> Weight = sum (weight, [])
>>> Weight
[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 (weighted)
100
>>> import random
>>> random.choice (range (1, 101), weights = weights)
[57]
>>> random.choice (range (1, 101), weights = weights)
[12]
>>> random.choice (range (1, 101), weights = 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.choice (range (1, 101), weights = weighted, 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
>>> 


2022-09-20 22:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.