python random.randint generate 0-100 on increments of 5

Viewed 3694

Is it possible to generate a random number with random.randint from 0-100 on increments of 5?

random.randint(0,100)

If its possible I need way to generate random integers from 0-100 but only with increments of 5.

2 Answers

Either use random.randrange() which lets you pick a step value:

random.randrange(0, 101, 5)

or just generate a number between 0 and 20 and multiply that number by 5:

random.randint(0, 20) * 5

I'm assuming here that 100 is a valid result. If 100 should not be generated, use random.randrange(0, 100, 5) or random.randint(0, 19) * 5.

If you wanted to generate a numpy array of such numbers using the numpy.random.randint() function (instead of the standard library random module version), just use

np.random.randint(0, 20, size=expected_size) * 5

The following code will generate random integer between 0 to 100

    import numpy as np
    random.choice(np.arange(0,101,5))
Related