
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Random Numbers Within a Given Range and Store in a List
In this article we will see how to generate Random numbers between a pair of numbers and finally store those values into list.
We use a function called randint. First let's have a look at its syntax.
Syntax
randint(start, end) Both start and end should be integers. Start should be less than end.
In this example we use the range function in a for lowith help of append we generate and add these random numbers to the new empty list.
Example
import random def randinrange(start, end, n): res = [] for j in range(n): res.append(random.randint(start, end)) return res # Number of random numbers needed n = 5 # Start value start = 12 #End value end = 23 print(randinrange(start, end, n))
Output
Running the above code gives us the following result −
[21, 20, 20, 17, 20]
Advertisements