randint()(randint())

python中的random模块用于生成随机数,而要生成随机整数则需要用到 random模块里的randint()函数。randint()函数随机产生括号里两个参数之间的整数,且包括这两个参数,划定随机生成整数的范围(最小最大值)。

1、random.randint()函数原型

random.randint(a, b)

用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b

2、使用语法

import random
r = random.randint(a, b)

随机产生a-b之间的整数,包括a和b。

3、使用

示例一:生成随机整数1~100

# -*- coding: UTF-8 -*-

import random

# 随机整数
print(random.randint(1, 100)

1函数的作用是,返回一个随机整型数,范围从低(包括)到高(不包括),即[low, high)。如果没有写参数high的值,则返回[0,low)的值。

参数如下:

low: int生成的数值最低要大于等于low。(hign = None时,生成的数值要在[0, low)区间内)high: int (可选)如果使用这个值,则生成的数值在[low, high)区间。size: int or tuple of ints(可选)输出随机数的尺寸,比如size = (m * n* k)则输出同规模即m * n* k个随机数。默认是None的,仅仅返回满足要求的单一随机数。dtype: dtype(可选):想要输出的格式。如int64、int等等输出:

out: int or ndarray of ints返回一个随机数或随机数数组例子>>> np.random.randint(2, size=10)array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])>>> np.random.randint(1, size=10)array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])>>> np.random.randint(5, size=(2, 4))array([[4, 0, 2, 1], [3, 2, 2, 0]])>>>np.random.randint(2, high=10, size=(2,3))array([[6, 8, 7], [2, 5, 2]])

————————

The random module in Python is used to generate random numbers. To generate random integers, you need to use the random() function in the random module. The randint() function randomly generates an integer between two parameters in parentheses, and includes these two parameters to delimit the range of randomly generated integers (minimum and maximum).

1、random.randint()函数原型

random.randint(a, b)

Used to generate an integer within a specified range. Where parameter a is the lower limit and parameter B is the upper limit. The generated random number n: A & lt= n <= b

< strong > 2. Use syntax < / strong >

import random
r = random.randint(a, b)

Randomly generate integers between a and B, including a and B.

< strong > 3. Use < / strong >

< strong > example 1: generate random integers 1 ~ 100 < / strong >

# -*- coding: UTF-8 -*-

import random

# 随机整数
print(random.randint(1, 100)

The 1 function returns a random integer from low (inclusive) to high (exclusive), that is, [low, high]. If there is no value for the high parameter, the value of [0, low) is returned.

The parameters are as follows:

Low: the minimum value generated by int must be greater than or equal to low. (when high = none, the generated value should be in the [0, low) range) high: int (optional) if this value is used, the generated value is in the [low, high) range. Size: int or tuple of ints (optional) outputs the size of the random number. For example, if size = (m * n * k), the same size, i.e. m * n * k random numbers, will be output. The default is none, and only a single random number meeting the requirements will be returned. Dtype: dtype (optional): the format you want to output. Such as Int64, int, etc

Out: int or ndarray of ints returns an example of a random number or array of random numbers & gt& gt;& gt; np. random. randint(2, size=10)array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])>& gt;& gt; np. random. randint(1, size=10)array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])>& gt;& gt; np. random. randint(5, size=(2, 4))array([[4, 0, 2, 1], [3, 2, 2, 0]])>& gt;& gt; np. random. randint(2, high=10, size=(2,3))array([[6, 8, 7], [2, 5, 2]])