Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Generate random number between x and y which is a multiple of 5 [duplicate]

Tags:

python

random

I've read the manual for pseudo-randomness in Python, and to my knowledge, you can only generate numbers up to a given maximum value, i.e. 0-1, 0-30, 0-1000, etc. I want to:

  • a) Generate a number between two ints, i.e. 5-55, and
  • b) Only include multiples of 5 (or those ending in 5 or 0, if that's easier)

I've looked around, and I can't find anywhere that explains this.

like image 766
tkbx Avatar asked Nov 27 '11 16:11

tkbx


People also ask

How do I make a list of random numbers without duplicates in Python?

To create a list of random numbers without duplicates with Python, we can use the random. sample method. We call random. sample with the range of numbers to generate and the number of random numbers to generate respectively.

What is difference between random () and Randint () function?

difference between random () and randint()The random command will generate a rondom value present in a given list/dictionary. And randint command will generate a random integer value from the given list/dictionary.


2 Answers

Create an integer random between e.g. 1-11 and multiply it by 5. Simple math.

import random for x in range(20):   print random.randint(1,11)*5, print 

produces e.g.

5 40 50 55 5 15 40 45 15 20 25 40 15 50 25 40 20 15 50 10 
like image 104
Has QUIT--Anony-Mousse Avatar answered Oct 01 '22 11:10

Has QUIT--Anony-Mousse


>>> import random >>> random.randrange(5,60,5) 

should work in any Python >= 2.

like image 20
mtrw Avatar answered Oct 01 '22 11:10

mtrw