-3

i made code to randomly generate weighted numbers and whenever i tell it to break when it generates a certain number it doesnt do it code:

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

i tried adding a while true command, i tried using

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

and also

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

and both dont work (1 being from the list and the other from the weights) so im not sure what else to try

yesno
  • 1
  • 2
  • 5
    You print `tapped` and can see that it is, as you requested, a **list** of 100 numbers. How could this list be equal to the number 500? Did you mean `if 500 in tapped`? – Thierry Lathuille Aug 30 '23 at 07:39
  • 2
    Note that this code is doing useless computations. First you could just pick `True`/`False` with probabilities `1/500`/`1-1/500` and use `any`, or even better use directly the [binomial law](https://en.wikipedia.org/wiki/Binomial_distribution) to compute the probability of at least one `True` with `p(True) = 1/500` for 100 repetitions. Don't loop when you can use math. – mozway Aug 30 '23 at 08:16

1 Answers1

0

try this instead:

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
Aymen Azoui
  • 369
  • 2
  • 4