我如何检测随机生成的数字

问题描述 投票:0回答:1

我编写了随机生成加权数字的代码,每当我告诉它在生成某个数字时中断时,它都不会这样做 代码:

import random
while True:
    numberList = [0, 100, 250, 500, 1000, 2000, 5000, 10000, 25000, 50000, 75000, 100000, 250000, 500000, 750000, 1000000, 5000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000]
    tapped = (random.choices(numberList, weights=(1, 1/25, 1/250, 1/500, 1/1000, 1/2000, 1/5000, 1/10000, 1/25000, 1/50000, 1/75000, 1/100000, 1/250000, 1/500000, 1/750000, 1/1000000, 1/5000000, 1/10000000, 1/100000000, 1/1000000000, 1/10000000000, 1/100000000000, 1/1000000000000, 1/10000000000000, 1/100000000000000, 1/1000000000000000), k=100))
    print(tapped)
    if tapped == 500:
        break

我尝试添加一段真实的命令,我尝试使用

if tapped = 500:
    print('you got a 500')

还有

if tapped = 1/500
    print('you got a 500')

两者都不起作用(一个来自列表,另一个来自权重)所以我不知道还能尝试什么

python random weighted
1个回答
0
投票

试试这个:

import random

numberList = [0, 100, 250, 500, 1000, 2000, 5000, 10000, 25000, 50000, 75000, 100000, 250000, 500000, 750000, 1000000, 5000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000]
weights = (1, 1/25, 1/250, 1/500, 1/1000, 1/2000, 1/5000, 1/10000, 1/25000, 1/50000, 1/75000, 1/100000, 1/250000, 1/500000, 1/750000, 1/1000000, 1/5000000, 1/10000000, 1/100000000, 1/1000000000, 1/10000000000, 1/100000000000, 1/1000000000000, 1/10000000000000, 1/100000000000000, 1/1000000000000000)

while True:
    tapped = random.choices(numberList, weights=weights, k=100)
    print(tapped)
    if 500 in tapped:
        print('you got a 500')
        break
© www.soinside.com 2019 - 2024. All rights reserved.