I am interested in only a selection of random number ,
in this case X % 5 == 0
.
我只对选择的随机数感兴趣,在这种情况下X%5 == 0。
from random import randint
num = randint(5,15000) #Inclusive
while not (num % 5 == 0):
num = randint(5,15000)
print num
here is what I have so far, but this does not seem like it's very pythonic.
Any suggestions?
这是我到目前为止所拥有的,但这似乎不是非常pythonic。有什么建议?
Thanks
2 个解决方案
#1
What about generate a random int r
in the range of 1 to MAX / Y
and use r * Y
?
如何生成1到MAX / Y范围内的随机int r并使用r * Y?
E.g.,
from random import randint
num = randint(1,15000 / 5) * 5
print num
#2
import random
MAX = 15000
n = 5
print n * random.randint(1, MAX / n)
#1
What about generate a random int r
in the range of 1 to MAX / Y
and use r * Y
?
如何生成1到MAX / Y范围内的随机int r并使用r * Y?
E.g.,
from random import randint
num = randint(1,15000 / 5) * 5
print num
#2
import random
MAX = 15000
n = 5
print n * random.randint(1, MAX / n)