本文共 6186 字,大约阅读时间需要 20 分钟。
如何在中生成0到9(含)之间的随机整数?
例如, 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
import print(random.randint(0,9))
random.randint(a, b)
返回一个随机整数N,使得a <= N <= b。
文档: :
from random import randintx = [randint(0, 9) for p in range(0, 10)]
这将生成范围为0到9(包括0和9)的10个伪随机整数。
通过random.shuffle
试试这个
>>> import random>>> nums = [x for x in range(10)]>>> random.shuffle(nums)>>> nums[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]
试试这个:
from random import randrange, uniform# randrange gives you an integral valueirand = randrange(0, 10)# uniform gives you a floating-point valuefrand = uniform(0, 10)
尝试:
from random import randrangeprint(randrange(10))
更多信息: :
模块是Python 3.6中的新功能。 这比用于加密或安全性使用的模块更好。
要在0-9的包含范围内随机打印一个整数:
from secrets import randbelowprint(randbelow(10))
有关详细信息,请参阅 。
如果你想使用numpy,那么使用以下内容:
import numpy as npprint(np.random.randint(0,10))
选择数组的大小(在本例中,我选择的大小为20)。 然后,使用以下内容:
import numpy as np np.random.randint(10, size=(1, 20))
您可以期望看到以下形式的输出( 每次运行时都会返回不同的随机整数;因此您可以预期输出数组中的整数与下面给出的示例不同 )。
array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
random.sample
是另一个可以使用的
import randomn = 1 # specify the no. of numbersnum = random.sample(range(10), n)num[0] # is the required number
最好的方法是使用导入随机功能
import randomprint(random.sample(range(10), 10))
或没有任何库导入:
n={} for i in range(10): n[i]=ifor p in range(10): print(n.popitem()[1])
这里从字典n
删除并返回一个任意值。
在连续数字的情况下, 或可能是最佳选择,但如果序列中有几个不同的值(即list
),您还可以使用 :
>>> import random>>> values = list(range(10))>>> random.choice(values)5
choice
也适用于非连续样本中的一个项目:
>>> values = [1, 2, 3, 5, 7, 10]>>> random.choice(values)7
如果你需要它“加密强大”,python 3.6和更新版本中也有一个 :
>>> import secrets>>> values = list(range(10))>>> secrets.choice(values)2
对于Python 3.6,我有更好的运气
str_Key = "" str_RandomKey = "" for int_I in range(128): str_Key = random.choice('0123456789') str_RandomKey = str_RandomKey + str_Key
只需添加“ABCD”和“abcd”或“^!〜= - > <”等字符即可更改要拉出的字符池,更改范围以更改生成的字符数。
>>> import random>>> random.randrange(10)3>>> random.randrange(10)1
要获取十个样本的列表:
>>> [random.randrange(10) for x in range(10)][9, 0, 4, 0, 5, 7, 4, 3, 6, 8]
虽然许多帖子演示了如何获得一个随机整数,但原始问题询问如何生成随机整数s (复数):
如何在Python中生成0到9(含)之间的随机整数?
为清楚起见,这里我们演示如何获得多个随机整数。
特定
>>> import randomlo = 0hi = 10size = 5
码
多个随机整数
# A>>> [random.randint(lo, hi) for _ in range(size)][9, 7, 0, 7, 3]
# B>>> [random.randrange(lo, hi) for _ in range(size)][8, 3, 6, 8, 7]
# C>>> lst = list(range(lo, hi))>>> random.shuffle(lst)>>> [lst[i] for i in range(size)][6, 8, 2, 5, 1]
# D>>> [random.choice(range(lo, hi)) for _ in range(size)][2, 1, 6, 9, 5]
随机整数的样本
# E>>> random.choices(range(lo, hi), k=size)[3, 2, 0, 8, 2]
# F>>> random.sample(range(lo, hi), k=size)[4, 5, 1, 2, 3]
细节
一些帖子演示了如何本机生成多个随机整数。 1以下是解决隐含问题的一些选项:
N
,使得a <= N <= b
randint(a, b+1)
别名 k
选择(替换,Python 3.6+) k
唯一选择(无替换): 2以下是标准库和Numpy中一些随机函数的比较:
| | random | numpy.random ||-|-----------------------|----------------------------------||A| randint(low, high) | randint(low, high) ||B| randrange(low, high) | randint(low, high) ||C| shuffle(seq) | shuffle(seq) ||D| choice(seq) | choice(seq) ||E| choices(seq, k) | choice(seq, size) ||F| sample(seq, k) | choice(seq, size, replace=False) |
您还可以快速将Numpy中的许多之一转换为随机整数样本。 3
例子
>>> np.random.normal(loc=5, scale=10, size=size).astype(int)array([17, 10, 3, 1, 16])>>> np.random.poisson(lam=1, size=size).astype(int)array([1, 3, 0, 2, 0])>>> np.random.lognormal(mean=0.0, sigma=1.0, size=size).astype(int)array([1, 3, 1, 5, 1])
1即@John Lawrence Aspden,@ ST Mohammed,@ SiddTheKid,@ user14372,@ zangw,et al。2 @prashanth提到这个模块显示一个整数。3由@Siddharth Satpathy演示
生成0到9之间的随机整数。
import numpyX = numpy.random.randint(0, 10, size=10)print(X)
输出:
[4 8 0 4 9 6 9 9 0 7]
我会尝试以下之一:
1.>
import numpy as npX1 = np.random.randint(low=0, high=10, size=(15,))print (X1)>>> array([3, 0, 9, 0, 5, 7, 6, 9, 6, 7, 9, 6, 6, 9, 8])
2.>
import numpy as npX2 = np.random.uniform(low=0, high=10, size=(15,)).astype(int)print (X2)>>> array([8, 3, 6, 9, 1, 0, 3, 6, 3, 3, 1, 2, 4, 0, 4])
3.>
from random import randrangeX3 = [randrange(10) for i in range(15)]print (X3)>>> [2, 1, 4, 1, 2, 8, 8, 6, 4, 1, 0, 5, 8, 3, 5]
4.>
from random import randintX4 = [randint(0, 9) for i in range(0, 15)]print (X4)>>> [6, 2, 6, 9, 5, 3, 2, 3, 3, 4, 4, 7, 4, 9, 6]
速度:
►np.random.randint是最快的 ,其次是np.random.uniform和random.randrange 。 random.randint是最慢的 。
►nap.random.randint和np.random.uniform 都比random.randrange和random.randint 快得多 ( 快 8到12倍)。
%timeit np.random.randint(low=0, high=10, size=(15,))>> 1.64 µs ± 7.83 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)%timeit np.random.uniform(low=0, high=10, size=(15,)).astype(int)>> 2.15 µs ± 38.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)%timeit [randrange(10) for i in range(15)]>> 12.9 µs ± 60.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)%timeit [randint(0, 9) for i in range(0, 15)]>> 20 µs ± 386 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
笔记:
1.> 在半开区间[低,高]上生成随机整数。
2.> 在半开区间[低,高]上生成均匀分布的数字。
3.> 从范围(开始,停止,步骤)生成随机数。
4.> 返回随机整数N,使得a <= N <= b。
5.> 将numpy数组转换为int数据类型。
6.>我选择了size =(15,)。 这将给你一个长度= 15的numpy数组。
这更像是一种数学方法,但它在100%的时间内起作用:
假设你想使用random.random()
函数在a
和b
之间生成一个数字。 要实现此目的,请执行以下操作:
num = (ba)*random.random() + a;
当然,您可以生成更多数字。
从模块的文档页面:
警告:不应将此模块的伪随机生成器用于安全目的。 如果需要加密安全的伪随机数生成器,请使用os.urandom()或SystemRandom。
,在Python 2.4中引入,被认为是 。 它仍然可以在Python 3.7.1中找到,这在撰写本文时是最新的。
>>> import string>>> string.digits'0123456789'>>> import random>>> random.SystemRandom().choice(string.digits)'8'>>> random.SystemRandom().choice(string.digits)'1'>>> random.SystemRandom().choice(string.digits)'8'>>> random.SystemRandom().choice(string.digits)'5'
而不是string.digits
, range
可以用于其他一些答案,或许可以理解。 根据您的需要混合搭配。
转载地址:http://gtogj.baihongyu.com/