 # Python | Generate random numbers within a given range and store in a list

## Python | Generate random numbers within a given range and store in a list

Given lower and upper limits, generate a given count of random numbers within a given range, starting from ‘start’ to ‘end’ and store them in list.
Examples:

```Input : num = 10, start = 20, end = 40
Output : [23, 20, 30, 33, 30, 36, 37, 27, 28, 38]
The output contains 10 random numbers in
range [20, 40].

Input : num = 5, start = 10, end = 15
Output : [15, 11, 15, 12, 11]
The output contains 5 random numbers in
range [10, 15].```

Python provides a random module to generate random numbers. To generate random numbers we have used the random function along with the use of the randint function.
Syntax:

`randint(start, end)`

randint accepts two parameters: a starting point and an ending point. Both should be integers and the first value should always be less than the second.

 `# Python code to generate` `# random numbers and` `# append them to a list` `import` `random` `# Function to generate` `# and append them` `# start = starting range,` `# end = ending range` `# num = number of` `# elements needs to be appended` `def` `Rand(start, end, num):` `    ``res ``=` `[]` `    ``for` `j ``in` `range``(num):` `        ``res.append(random.randint(start, end))` `    ``return` `res` `# Driver Code` `num ``=` `10` `start ``=` `20` `end ``=` `40` `print``(Rand(start, end, num))`

Output:

`[23, 20, 30, 33, 30, 36, 37, 27, 28, 38]`

Method 2:  Using numpy random.randint() method to generate randoam numbers.

 `# Python code to generate` `# random numbers and` `# append them to a list` `import` `numpy as np` `def` `Rand(start, end, num):` `    ``res ``=` `[]` `    ``for` `j ``in` `range``(num):` `        ``res.append(np.random.randint(start, end))` `    ``return` `res` `# Driver Code` `num ``=` `10` `start ``=` `20` `end ``=` `40` `print``(Rand(start, end, num))`

Output:

`[30, 30, 38, 39, 39, 37, 24, 25, 28, 32]`

Last Updated on March 1, 2022 by admin

## Oracle Database Connection in PythonOracle Database Connection in Python

Oracle Database Connection in Python Sometimes as part of programming, we required to work with

## Find the length of a set in PythonFind the length of a set in Python

Find the length of a set in Python In Python, a Set is a collection data type that

## First Class functions in PythonFirst Class functions in Python

First Class functions in Python First class objects in a language are handled uniformly throughout. They

## How to Convert Int to Bytes in Python?How to Convert Int to Bytes in Python?

How to Convert Int to Bytes in Python? An int object can be used to

## Function Annotations in PythonFunction Annotations in Python

Function Annotations in Python PEP: PEP stands for Python Enhancement Proposal. It is a design document

## Billion To Rupees CalculatorBillion To Rupees Calculator

Billion to Rupees Calculator Billion: Calculate Rupees: Convert Billions to Rupees with Online Calculator Looking

## XGBoost for RegressionXGBoost for Regression

XGBoost for Regression The results of the regression problems are continuous or real values. Some

## Pandas Dataframe.sort_values()Pandas Dataframe.sort_values()

Python | Pandas Dataframe.sort_values() Python is a great language for doing data analysis, primarily because