随机数生成器python

We can use Python random module to generate random numbers. We can use this module to generate random integers, floating-point numbers, or a sequence of random numbers.

我们可以使用Python 随机模块生成随机数。 我们可以使用该模块生成随机整数,浮点数或随机数序列。

1.用Python生成随机整数 (1. Generating Random Integer in Python)

Python random module randint() function can generate a random number between the given endpoints.

Python随机模块randint()函数可以在给定的端点之间生成一个随机数。

>>> from random import randint
>>> 
>>> randint(1, 10)
6
>>> randint(1, 10)
8
>>> randint(1, 10)
2

Note that the randint() includes both endpoints while generating the random integer. If you don’t want the second endpoint to be included, then use randrange() function.

请注意,在生成随机整数时, randint()包括两个端点。 如果您不希望包含第二个端点,请使用randrange()函数。

>>> from random import randrange
>>> 
>>> randrange(1, 10)
8
>>>

2.在Python中生成随机浮点数 (2. Generating Random Float in Python)

The random module has range() function that generates a random floating-point number between 0 (inclusive) and 1 (exclusive).

random模块具有range()函数,该函数生成介于0(含)和1(不含)之间的随机浮点数。

>>> import random
>>> 
>>> random.random()
0.5453202789895193
>>> random.random()
0.9264563336754832
>>>

There is no separate method to generate a floating-point number between a given range. For that, just multiply it with the desired range.

没有给定范围之间生成浮点数的单独方法。 为此,只需将其乘以所需的范围即可。

Here is a simple program to generate a random floating-point number between 0 and 100.

这是一个简单的程序,用于生成0到100之间的随机浮点数。

>>> random.random() * 100
28.226855764270553
>>> random.random() * 100
41.844280268733115
>>>

3.从序列中选择一个随机数 (3. Chosing a Random Number from a Sequence)

We can also use the random module to select a random number from a sequence using the choice() method.

我们还可以使用choice模块使用choice()方法从序列中选择一个随机数。

>>> from random import choice
>>> nums = [1,3,5,7,9,11]
>>> choice(nums)
5
>>> choice(nums)
7
>>> choice(nums)
3
>>> choice(nums)
3
>>>

4.快速随机种子 (4. A quick word on Random Seed)

When we call random module functions, it initializes the Random class internally and uses the current system time to generate the seed value. This seed value is then used to generate random numbers.

当我们调用随机模块函数时,它将在内部初始化Random类,并使用当前系统时间生成种子值。 然后将此种子值用于生成随机数。

We can also set the random seed, in that case, the same sequence of random numbers will get generated every time.

我们还可以设置随机种子,在这种情况下,每次都会生成相同的随机数序列。

Let’s understand this behavior with a simple example.

让我们通过一个简单的例子来了解这种行为。

>>> import random
>>> 
>>> random.seed(1000)
>>> 
>>> random.random()
0.7773566427005639
>>> random.random()
0.6698255595592497
>>> random.random()
0.09913960392481702
>>> 
>>> random.seed(1000)  # resetting the seed again to 1000
>>> 
>>> random.random()
0.7773566427005639
>>> random.random()
0.6698255595592497
>>> random.random()
0.09913960392481702
>>>

Notice that the random numbers generated each time are following the same sequence. Unless you want something like this, try to avoid setting the random seed value.

请注意,每次生成的随机数都遵循相同的顺序。 除非您想要这样,否则请避免设置随机种子值。

翻译自: https://www.journaldev/31916/random-number-generator-in-python

随机数生成器python

更多推荐

随机数生成器python_Python中的随机数生成器