jeudi 17 mai 2018

Why does my random number generator return "none" sometimes? [duplicate]

This question already has an answer here:

Below is a recursive function I wrote to generate random numbers in a range. The trouble is, it sometimes returns "none". Why is that?

import random

# Here, 'data' is a list of images. It contains about 20000 images.
# But I want to pick an image at random within index 13338, not beyond.
# hence the need for this function.

def choose_random_image(data):
    i = random.choice(data.index)
    if i in range(0,13338):
        return i
    else:
        choose_random_image(data)

i = choose_random_image(dataset)

print(i)

It works about roughly 80-90% of the time, and the other 10-20% of the time, it prints "none".




Aucun commentaire:

Enregistrer un commentaire