Python:生成x和y之间的随机数,该数字是5的倍数 [英] Python: Generate random number between x and y which is a multiple of 5

查看:772
本文介绍了Python:生成x和y之间的随机数,该数字是5的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了Python伪随机手册,据我所知,您只能生成不超过给定最大值的数字,即0-1、0-30、0-1000等.我想要到:

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)生成两个整数(即5-55)之间的数字,并且
  • b)仅包括5的倍数(如果更简单,则以5或0结尾的数字)

我环顾四周,找不到任何可以解释这一点的地方.

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

推荐答案

创建一个整数,例如介于1-11乘以5.简单的数学.

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

产生例如

5 40 50 55 5 15 40 45 15 20 25 40 15 50 25 40 20 15 50 10

这篇关于Python:生成x和y之间的随机数,该数字是5的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆