首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >生成随机浮点数

生成随机浮点数
EN

Stack Overflow用户
提问于 2015-03-28 06:01:54
回答 3查看 4.5K关注 0票数 2

现在,我正在您的屏幕上制作Python "type“(我感到无聊和实验性),而且我自己已经做了这么多,但是我找不到在脚本中使用小数的任何方法,因为Python只接受randint()的整组数字。

代码语言:javascript
复制
#include <stdio.h>;
import sys;
import time;
import os;
import random;
os.system("clear");

script = "I just really need to type something right now. I'm not sure why, but I really feel like typing on the Chromebook right now. It seems kinda weird, but I like doing this. It helps me practice what I'm doing. I've noticed that I'm a lot worse at typing lately, and I don't know why. Maybe it's because I've been working with a chunky keyboard, and these Chromebook keyboards are extremely thin. Yup, this is actually entertaining for me. I'm not exactly sure why I want to do this, but I just do. It's almost addicting to me, and I don't know why.\n";

for i in range(0, 587):
sys.stdout.write(str(scripti));
sys.stdout.flush();
time_num = random.randint(0.1, 0.4);
time.sleep(time_num);

我得到的输出是:

代码语言:javascript
复制
ITraceback (most recent call last):
File "main.py", line 11, in <module>
time_num = random.randint(0.1, 0.4);
File "/usr/lib64/python2.7/random.py", line 242, in randint
return self.randrange(a, b+1)
File "/usr/lib64/python2.7/random.py", line 187, in randrange
raise ValueError, "non-integer arg 1 for randrange()"
ValueError: non-integer arg 1 for randrange()

有什么想法吗?

编辑:不要告诉我如何使这更简单,因为这只是为了学习。

EN

Stack Overflow用户

回答已采纳

发布于 2015-03-28 06:07:00

你为什么不用随机均匀分布代替?在这种情况下,您可以获得非整数值。使用:

代码语言:javascript
复制
import random
random.uniform(0.1,0.4)
票数 8
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29314213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档