Python 生成 0 到 9 之间的随机整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3996904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate random integers between 0 and 9
提问by aneuryzm
How can I generate random integers between 0 and 9 (inclusive) in Python?
如何在 Python 中生成 0 到 9(含)之间的随机整数?
For example, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
例如, 0, 1, 2, 3, 4, 5, 6, 7, 8,9
采纳答案by kovshenin
Try:
尝试:
from random import randrange
print(randrange(10))
More info: http://docs.python.org/library/random.html#random.randrange
更多信息:http: //docs.python.org/library/random.html#random.randrange
回答by Andrew Hare
Try this:
尝试这个:
from random import randrange, uniform
# randrange gives you an integral value
irand = randrange(0, 10)
# uniform gives you a floating-point value
frand = uniform(0, 10)
回答by JMSamudio
import random
print(random.randint(0,9))
random.randint(a, b)
Return a random integer N such that a <= N <= b.
返回一个随机整数 N,使得 a <= N <= b。
Docs: https://docs.python.org/3.1/library/random.html#random.randint
文档:https: //docs.python.org/3.1/library/random.html#random.randint
回答by user14372
from random import randint
x = [randint(0, 9) for p in range(0, 10)]
This generates 10 pseudorandom integers in range 0 to 9 inclusive.
这将生成 0 到 9 范围内的 10 个伪随机整数。
回答by zangw
Try this through random.shuffle
试试这个 random.shuffle
>>> import random
>>> nums = range(10)
>>> random.shuffle(nums)
>>> nums
[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]
回答by Chris_Rands
The secretsmodule is new in Python 3.6. This is better than the randommodule for cryptography or security uses.
该secrets模块是 Python 3.6 中的新模块。这比random用于加密或安全用途的模块更好。
To randomly print an integer in the inclusive range 0-9:
随机打印 0-9 之间的整数:
from secrets import randbelow
print(randbelow(10))
For details, see PEP 506.
有关详细信息,请参阅PEP 506。
回答by sushmit
if you want to use numpy then use the following:
如果您想使用 numpy,请使用以下命令:
import numpy as np
print(np.random.randint(0,10))
回答by Commoner
Choose the size of the array (in this example, I have chosen the size to be 20). And then, use the following:
选择数组的大小(在本例中,我选择的大小为 20)。然后,使用以下内容:
import numpy as np
np.random.randint(10, size=(1, 20))
You can expect to see an output of the following form (different random integers will be returned each time you run it; hence you can expect the integers in the output array to differ from the example given below).
您可以期望看到以下形式的输出(每次运行时将返回不同的随机整数;因此您可以期望输出数组中的整数与下面给出的示例不同)。
array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
回答by prashanth
random.sampleis another that can be used
random.sample是另一个可以使用的
import random
n = 1 # specify the no. of numbers
num = random.sample(range(10), n)
num[0] # is the required number
回答by S T Mohammed
Best way is to use import Random function
最好的方法是使用 import Random 函数
import random
print(random.sample(range(10), 10))
or without any library import:
或没有任何库导入:
n={}
for i in range(10):
n[i]=i
for p in range(10):
print(n.popitem()[1])
here the popitemsremoves and returns an arbitrary value from the dictionary n.
这里popitems从字典中删除并返回一个任意值n。

