python生成6位随机数_用Python生成随机数的6种方法

python生成6位随机数_用Python生成随机数的6种方法_第1张图片

python生成6位随机数

In this tutorial, you will see how we can generate random number in Python.

在本教程中,您将看到我们如何在Python中生成随机数。

Random number means we can’t predict that which number will be returned or printed. Let’s see some different ways in python to generate random number.

随机数意味着我们无法预测将返回或打印哪个数字。 让我们看看python中生成随机数的一些不同方法。

在Python中生成随机数的方法 (Ways to Generate Random Number in Python)

1.使用random.randint() (1. using random.randint())

from random import randint
print(randint(1,100))

We can not predict the output here. The output will be different every time we execute this program. But we can predict that it will be a number from 1 to 100.

我们无法在这里预测输出。 每次执行此程序时,输出都会不同。 但是我们可以预测,它将是一个1到100的数字。

Let’s say we want a random number but it should be multiple of 5.

假设我们要一个随机数,但它应该是5的倍数。

from random import randint
print(randint(1,20)*5)

It will print a random number from 0 to 100, but it will be a multiple of 5. In above program random.randint(1,20) will generate a number from 1 to 20, after that we’re multiplying that generated number with 5. So that the minimum number that can be printed is 1*5 = 5 and maximum can be 20*5 = 100. 

它将打印一个从0到100的随机数,但是它将是5的倍数。在上面的程序中random.randint(1,20)将生成一个从1到20的数字,之后,我们将生成的数字乘以5.这样可以打印的最小数量为1 * 5 = 5,最大数量为20 * 5 = 100。  

2.使用random.randrange() (2. using random.randrange())

from random import randrange
print(randrange(1,10))

The output will be same as randint() method. But the only difference is randint(1,100) generates a number from 1 to 100 , but randrange(1, 100) will generate from 1 to 99. On other hand we can pass single argument in randrange() method where it is necessary to pass two arguments in randint() method.

输出将与randint()方法相同。 但是唯一的区别是randint(1,100)生成一个从1到100的数字,而randrange(1,100)则生成一个从1到99的数字。另一方面,我们可以在randrange()方法中传递需要传递的单个参数randint()方法中有两个参数。

from random import randrange
print(randrange(100))

It will print any number from 0 to 99.

它将打印0到99之间的任何数字。

3.使用random.uniform() (3. using random.uniform())

from random import uniform
print(uniform(1,10))

Output will be a random floating point value between 1 and 10, i.e., 6.48754287679. So if you want a random number that should be a floating type value then uniform() method could be best choice.

输出将是介于1和10之间的随机浮点值,即6.48754287679。 因此,如果您想要一个随机数,它应该是一个浮点型值,那么uniform()方法可能是最佳选择。

But still you can get the integer number by parsing the generated value by uniform method into int as mentioned in program below.

但是仍然可以通过统一方法将生成的值解析为int来获得整数,如下面的程序中所述。

from random import uniform
print(int(uniform(1,10)))

Output will be a random integer value.

输出将是一个随机整数值。

4.使用random.choice() (4. using random.choice())

If we want to choose a random number from a set of given numbers like 23, 43, 34, 12, 35, 12 then choice() method will be the best option here.

如果我们要从一组给定的数字(例如23、43、34、12、35、12)中选择一个随机数,那么choice()方法将是最好的选择。

from random import choice
numbers = [23,43,34,12,35,12]
print(choice(numbers))

Output will be a number from the given list of numbers.

输出将是给定数字列表中的一个数字。

5.使用secrets.randbelow() (5. using secrets.randbelow())

The secrets module is new in Python 3.6. This is better than the random module for cryptography or security issues. Below program randomly print an integer in the inclusive range 0 to 9 is given below.

secrets模块是Python 3.6中的新增功能。 对于加密或安全性问题,这比随机模块要好。 下面的程序在下面给出随机打印范围为0到9的整数。

from secrets import randbelow
print(randbelow(10))

6.使用secrets.choice() (6. using secrets.choice())

Like random.choice(), there is another method available in secrets module which can be used to get a random number from a given list.

像random.choice()一样,secrets模块中还有另一种方法可用于从给定列表中获取随机数。

from secrets import choice
numbers = [12,323,44,34,54]
print(choice(numbers))

Output will be any one number from given list numbers.

输出将是给定列表编号中的任何一个编号。

Comment below if you’ve any question related to python random number generator.

如果您有任何与python随机数生成器有关的问题,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2018/04/generate-random-number-in-python.html

python生成6位随机数

你可能感兴趣的:(python,random,java,机器学习,人工智能)