Python 入门简明教程
1. 1. 与 Python 的第一次亲密接触-写给初学者 2. 2. Python 语法关键字 3. 3. Python 的注释和语法 4. 4. Python 基本数据类型 5. 5. Python 输入输出 6. 6. Python 运算符 7. 7. Python 变量 8. 8. Python 命名空间和变量生存范围 9. 9. Python 逻辑判断 10. 10. Python For 循环语法 11. 11. Python While 循环语法 12. 12. Python 循环语法中 break 与 continue的使用 13. 14. Python 三目运算符 14. 15. Python 循环语法中 pass 语句的使用 15. 16. Python 循环语法示例 16. 17. Python 函数 17. 18. Python main 函数 18. 19. Python print 函数 19. 20. Python print format 输出格式化 20. 21. Python print 输出到文件 21. 22. Python 输入 22. 23. Python import 导入模块 23. 25. Python 中递归的使用 24. 26. Python 匿名函数 25. 27. Python lambda 26. 28. Python PIP 包管理机制 27. 29. Python 命令行参数 28. 30. Python Numbers 包 29. 31. Python 随机数 30. 32. Python String 转 int 31. 34. Python 自定义异常 Exception 32. 35. Python 继承 33. 36. Python 父类 34. 38. Python 操作符重载 35. 39. Python __str__ and __repr__

31. Python 随机数

在本教程中,我们将学习Python随机数。在之前的教程中,我们了解到Python数学模块.

Python随机数

要使用python随机数,我们需要导入python的随机首先是模块。Python随机模块提供伪随机性。

Python随机模块使用mersennetwister作为核心随机生成器。因此,这个模块完全不适合用于密码目的,因为它是确定性的。但是,在大多数情况下,我们可以使用Python的random模块,因为Python的random模块包含许多众所周知的随机分布。

Python随机整数

在本节中,我们将讨论随机生成整数。我们可以利用randint(a,b) function to get a random integer from range a to b. Again, we can get number from a sequence by using randrange(start, stop, step) function. Let’s see an example to get python random integer.


import random as rand

a = 10
b = 100
print("
a =", a, "and b =", b)
print("printing number [", a, ", ", b, ") :", rand.randint(a,b))

start = 1
stop = 12
step = 2
print("
sequence = [1, 3, 5, 7, 9, 11]")
print("printing one number from the sequence :", rand.randrange(start, stop, step))

每次运行时,输出都会改变。但是,这里给出了一个示例输出。

Python随机浮点

有几个函数可以随机返回实数或浮点。例如,random() function returns a real number from 0 to 1 (exclusive).

再一次,uniform(a, b) functions return a real number from a to b. Moreover there are some random distributions also available in Python random module. We can also get real number from those distribution.

我们可以从指数分布中得到随机数指数变量(lambd)功能。


import random as rand

print("Random number from 0 to 1 :", rand.random())
print("Uniform Distribution [1,5] :", rand.uniform(1, 5))
print("Gaussian Distribution mu=0, sigma=1 :", rand.gauss(0, 1))
print("Exponential Distribution lambda = 1/10 :", rand.expovariate(1/10))

每次执行时,输出中的值都会有所不同。你会得到这样的输出。


Random number from 0 to 1 : 0.5311529501408693
Uniform Distribution [1,5] : 3.8716411264052546
Gaussian Distribution mu=0, sigma=1 : 0.8779046620056893
Exponential Distribution lambda = 1/10 : 1.4637113187536595

Python随机种子

Python随机数生成是基于前一个数字的,因此使用系统时间是一个很好的方法,可以确保程序每次运行时都生成不同的数字。

我们可以使用python random seed()函数来设置初始值。请注意,如果我们的种子值在每次执行中不发生变化,我们将得到相同的数字序列。下面是一个示例程序来证明这个关于种子值的理论。


import random

random.seed(10)

print("1st random number = ", random.random())
print("2nd random number = ", random.random())
print("1st random int = ", random.randint(1, 100))
print("2nd random int = ", random.randint(1, 100))

# resetting the seed to 10 i.e. first value
random.seed(10)

print("3rd random number = ", random.random())
print("4th random number = ", random.random())
print("3rd random int = ", random.randint(1, 100))
print("4th random int = ", random.randint(1, 100))

下图显示了python随机种子示例程序生成的输出。我们将得到相同的随机数序列。

Python随机列表&8211;choice(),shuffle(),sample()

有一些函数可以在序列中使用随机性。例如,使用choice() function you can get a random element from a sequence.

再次,使用shuffle() function you can shuffle the elements in a sequence.

另外,使用sample() function you can get 随机序列中的元素数。因此,让我们看看下面的随机列表示例代码。


import random as rand

# initialize sequences
string = "inconvenience"
l = [1, 2, 3, 4, 10, 15]

# get a single element randomly
print("Single character randomly chosen :", rand.choice(string))
print("one randomly chosen number :", rand.choice(l))

# get multiple element
print("Randomly chosen 4 character from string :", rand.sample(string, 4))
print("Randomly chosen 4 length list :", rand.sample(l, 4))

# shuffle the list
rand.shuffle(l)
print("list is shuffled :", l)  # print the list

您可以得到如下输出。


Single character randomly chosen : i
one randomly chosen number : 10
Randomly chosen 4 character from string : ["e", "c", "n", "n"]
Randomly chosen 4 length list : [2, 10, 3, 15]
list is shuffled : [2, 4, 15, 3, 10, 1]

这就是python随机数。要了解更多,请看他们的官方文件.