如何使用python 3.2生成(并标记)随机整数? [英] How do I generate (and label) a random integer with python 3.2?
问题描述
好吧,所以我确实是编程的新手,但我无法确定如何让python v3.2在我给出的参数之间生成一个随机正整数。只是这样你才能理解上下文,我正在尝试创建一个猜测游戏,用户输入参数(例如1到50),然后计算机在给定数字之间生成一个随机数。然后,用户必须猜测计算机选择的值。我搜索得很长很难,但我能找到的所有解决方案只告诉一个如何获得早期版本的python来生成一个随机整数。尽管我已经知道了,v.3.2改变了生成和标记随机整数的方法。有人知道怎么做吗?
Okay, so I'm admittedly a newbie to programming, but I can't determine how to get python v3.2 to generate a random positive integer between parameters I've given it. Just so you can understand the context, I'm trying to create a guessing-game where the user inputs parameters (say 1 to 50), and the computer generates a random number between the given numbers. The user would then have to guess the value that the computer has chosen. I've searched long and hard, but all of the solutions I can find only tell one how to get earlier versions of python to generate a random integer. As near as I can tell, v.3.2 changed how to generate and label a random integer. Anyone know how to do this?
推荐答案
使用 random.randrange 或 random.randint (注意链接是Python 3k文档)。
Use random.randrange or random.randint (Note the links are to the Python 3k docs).
In [67]: import random
In [69]: random.randrange(1,10)
Out[69]: 8
这篇关于如何使用python 3.2生成(并标记)随机整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!