TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Generate Random Number

Python Generate Random Number for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.

<< Back to PYTHON

Python Program to Generate a Random Number

In Python programming, you can generate a random integer, doubles, longs etc . in various ranges by importing a "random" class.

In Python, we can generate a random integer, doubles, long, etc in various ranges by importing a "random" module. In the following example, we will learn how to generate random numbers using the random module.

Syntax:

First, we have to import the random module and then apply the syntax:

import random

Generating a Random Number

The random module provides a random() method which generates a float number between 0 and 1. Let's see the following example.

Example -

import random
n = random.random()
print(n)

Output:

0.7632870997556201

If we run the code again, we will get the different output as follows.

0.8053503984689108

Generating a Number within a Given Range

Python random module provides the randint() method that generates an integer number within a specific range. We can pass the two numbers as arguments that defines the range. Let's understand the following example.

Example - 1:

import random
n = random.randint(0,50)
print(n)

Output:

40

Example - 2:

import random
n = random.randint(100, 200)
print(n)

Output:

143

Using for loop

The randint() method can be used with for loop to generated a list of random numbers. To do so, we need to create an empty list, and then append the random numbers generated to the empty list one by one. Let's understand the following example.

Example -

import random
rand_list = []
for i in range(0,10):
    n = random.randint(1,50)
    rand_list.append(n)
print(rand_list)

Output:

[10, 49, 16, 31, 45, 21, 19, 32, 30, 16]

Using random.sample()

The random module also provides the sample() method, which directly generates a list of random numbers. Below is the example of generating random numbers using the sample() method.

Example -

import random
#Generate 5 random numbers between 10 and 30
random_list = random.sample(range(10, 40), 6)
print(random_list)

Output:

[18, 25, 26, 29, 14, 31]

In the above code, we have used the range() function, which generates the numbers between the given range.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf