随机数⽣成器python_Python中的随机数⽣成器
随机数⽣成器python
We can use Python 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.
python生成1到100之间随机数请注意,每次⽣成的随机数都遵循相同的顺序。 除⾮您想要这样,否则请避免设置随机种⼦值。
翻译⾃:
随机数⽣成器python
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论