如何在Python中生成介于0和9(含)之间的随机整数?

例如,0、1、2、3、4、5、6、7、8、9


当前回答

>>> 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]

其他回答

我会尝试以下方法之一:

1.>numpy.random.randint

import numpy as np
X1 = 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.>numpy.random.uniform

import numpy as np
X2 = 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.>numpy.random.ochoice

import numpy as np
X3 = np.random.choice(a=10, size=15 )

print (X3)
>>> array([1, 4, 0, 2, 5, 2, 7, 5, 0, 0, 8, 4, 4, 0, 9])

4.>随机范围

from random import randrange
X4 = [randrange(10) for i in range(15)]

print (X4)
>>> [2, 1, 4, 1, 2, 8, 8, 6, 4, 1, 0, 5, 8, 3, 5]

5.>随机随机

from random import randint
X5 = [randint(0, 9) for i in range(0, 15)]

print (X5)
>>> [6, 2, 6, 9, 5, 3, 2, 3, 3, 4, 4, 7, 4, 9, 6]

速度:

► np.random.uniform和np.randm.randint比np.rando.choice、random.randrange和random.randint快得多(大约快10倍)。

%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 np.random.choice(a=10, size=15 )
>> 21 µs ± 629 ns per loop (mean ± std. dev. of 7 runs, 10000 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.>np.random.randint在半开区间(低,高)上生成随机整数。2.>np.random.uniform在半开区间(低,高)上生成均匀分布的数。3.>np.randomchoice在半开区间(低,高)上生成一个随机样本,就好像参数a是np.arange(n)一样。4.>randrange(stop)从range(start,stop,step)生成一个随机数。5.>randint(a,b)返回随机整数N,使得a<=N<=b。6.>astype(int)将numpy数组强制转换为int数据类型。我选择了size=(15,)。这将为您提供长度为15的numpy数组。

OpenTURNS不仅可以模拟随机整数,还可以使用UserDefined定义的类定义关联的分布。

以下模拟了分布的12个结果。

import openturns as ot
points = [[i] for i in range(10)]
distribution = ot.UserDefined(points) # By default, with equal weights.
for i in range(12):
    x = distribution.getRealization()
    print(i,x)

这将打印:

0 [8]
1 [7]
2 [4]
3 [7]
4 [3]
5 [3]
6 [2]
7 [9]
8 [0]
9 [5]
10 [9]
11 [6]

括号在那里,因为x是一维中的一个点。在对getSample的一次调用中生成12个结果会更容易:

sample = distribution.getSample(12)

将产生:

>>> print(sample)
     [ v0 ]
 0 : [ 3  ]
 1 : [ 9  ]
 2 : [ 6  ]
 3 : [ 3  ]
 4 : [ 2  ]
 5 : [ 6  ]
 6 : [ 9  ]
 7 : [ 5  ]
 8 : [ 9  ]
 9 : [ 5  ]
10 : [ 3  ]
11 : [ 2  ]

有关此主题的更多详细信息,请参见:http://openturns.github.io/openturns/master/user_manual/_generated/openturns.UserDefined.html

您需要随机python模块,它是标准库的一部分。使用代码。。。

from random import randint

num1= randint(0,9)

这将将变量num1设置为介于0和9之间的随机数(包括0和9)。

如果要使用numpy,请使用以下命令:

import numpy as np
print(np.random.randint(0,10))
>>> 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]