Python中的随机数生成器

本文介绍了Python中的随机数生成,包括使用`random`库生成0到1之间的浮点数,使用`randint`生成指定范围内的整数,如0到10和0到100,以及生成指定范围的浮点数。此外,还讲解了如何从给定列表中随机选择元素。
摘要由CSDN通过智能技术生成

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()
Generate Random Number Between 0 and 1
Generate Random Number Between 0 and 1
生成0到1之间的随机数

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)
Generate Random Number Between 0 and 10
Generate Random Number Between 0 and 10
生成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 Number Between 0 and 100
Generate Random Number Between 0 and 100
生成0到100之间的随机数

生成指定范围内的随机浮点数(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.07.0之间的浮动随机数。 在这种情况下,我们将uniform职能。

from random import uniform
uniform(5.0,7.0)

Generate Random Floating Number In Specified Range
Generate Random Floating Number In Specified Range
生成指定范围内的随机浮点数

从给定列表中选择随机元素(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 ,它从给定列表中随机选择元素。 我们只提供列表,选定的元素将被返回。 在此示例中,我们将使用onetwothree作为列表。

from random import choice
choice(['one','two','three'])
Select Random Element From Given List
Select Random Element From Given List
从给定列表中选择随机元素
LEARN MORE  How To Shuffle/Randomize Array in PHP
了解更多如何在PHP中随机播放/随机化数组

翻译自: https://www.poftut.com/random-number-generator-python/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值