概述
Python is practical language which provides different functions in practical way. Random number are generally used in security related issues but there are other areas too. In this post we will look different random number generation examples about python.
Python是实用的语言,以实用的方式提供了不同的功能。 随机数通常用于与安全性相关的问题,但也有其他领域。 在这篇文章中,我们将寻找有关python的不同随机数生成示例。
生成0到1之间的随机数 (Generate Random Number Between 0 and 1)
Python provides a library named random
by default. This library is used to provide different type of random functions according to given parameters. We will use random
function in order to generate random numbers in this example. This function generates floating point values between 0 and 1
Python默认提供一个名为random
的库。 该库用于根据给定的参数提供不同类型的随机函数。 在此示例中,我们将使用random
函数来生成随机数。 此函数生成介于0和1之间的浮点值
from random import random
random()
As we can see produces random numbers are like 0.476250741043003
如我们所见,产生随机数就像0.476250741043003
生成随机数介于0到10之间的随机数 (Generate Random Number with Randint Between 0 and 10)
What if we need to generate integer numbers in specified range. We can not use random
function in a practical and efficient way. We will use randint
function in order to generate random numbers between 1 and 10 by specifying range.
如果我们需要生成指定范围内的整数怎么办。 我们不能以实用和有效的方式使用random
函数。 我们将使用randint
函数,以通过指定范围来生成1到10之间的随机数。
from random import randint
randint(0,10)
We can see that generated numbers are between 1 and 10
我们可以看到生成的数字在1到10之间
生成随机数介于0到100之间的随机数(Generate Random Number with Randint Between 0 and 100)
Another useful example is generating random integers between 0 and 100. As you see we can change the range start and end whatever we want.
另一个有用的示例是生成0到100之间的随机整数。如您所见,我们可以根据需要更改范围的开始和结束。
from random import randint
randint(0,10)
生成指定范围内的随机浮点数(Generate Random Floating Number In Specified Range)
We have generated floating random number in 0 and 1 . But we may need more options about the range. For example we may need to generate floating random number between 5.0
and 7.0
. In this situations we will us uniform
function.
我们在0和1中生成了浮动随机数。 但是我们可能需要有关范围的更多选择。 例如,我们可能需要生成介于5.0
和7.0
之间的浮动随机数。 在这种情况下,我们将uniform
职能。
from random import uniform
uniform(5.0,7.0)
从给定列表中选择随机元素(Select Random Element From Given List)
Another useful function is choice
which selects element from given list randomly. We just provide the list and selected element will be returned. In this example we will use one
, two
and three
as a list.
另一个有用的功能是choice
,它从给定列表中随机选择元素。 我们只提供列表,选定的元素将被返回。 在此示例中,我们将使用one
, two
和three
作为列表。
from random import choice
choice(['one','two','three'])
翻译自: https://www.poftut.com/random-number-generator-python/
最后
以上就是义气悟空为你收集整理的Python中的随机数生成器 生成0到1之间的随机数 (Generate Random Number Between 0 and 1) 生成随机数介于0到10之间的随机数 (Generate Random Number with Randint Between 0 and 10)生成随机数介于0到100之间的随机数(Generate Random Number with Randint Between 0 and 100)生成指定范围内的随机浮点数(Generate Random Floatin的全部内容,希望文章能够帮你解决Python中的随机数生成器 生成0到1之间的随机数 (Generate Random Number Between 0 and 1) 生成随机数介于0到10之间的随机数 (Generate Random Number with Randint Between 0 and 10)生成随机数介于0到100之间的随机数(Generate Random Number with Randint Between 0 and 100)生成指定范围内的随机浮点数(Generate Random Floatin所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复