## Python `random`  : `randint()`

In this article, we will see how to use Python  `random.randint()` functions to generate a random number.
randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions and one of them being able to generate random numbers, which is randint().
The `randint()` method returns an integer number selected element from the specified range.

## Syntax :

random.randint(startstop)

## Parameter Values :

ParameterDescription
startRequired. An integer specifying at which position to start.
stopRequired. An integer specifying at which position to end.

#### A random integer in range [start, end] including the end points.

This function takes two parameters. Both are mandatory. For example, `random.randint(0, 100)` will return any random number between 0 to 100.

Note:

The `randint(start, stop)` includes both start and stop numbers while generating random integer. It will generate a random number from the inclusive range.

Here in the following example, we are trying to print a random int in a given range using randint() method :

`# Python3 program explaining work of randint() function   # imports random module import random   # Generates a random number between a given positive range r1 = random.randint(0, 100) print("Random number between 0 and 100 is % d" % (r1))   # Generates a random number between  two given negative ranger2 = random.randint(-100, 0) print("Random number between -100 and 0 is % d" % (r2))   # Generates a random number between  a positive and a negative ranger3 = random.randint(-500, 500) print("Random number between -500 and 500 is % d" % (r3)) `
Output :
Random number between 0 and 100 is  15
Random number between -100 and 0 is -22
Random number between -500 and 500 is  476

Errors and Exceptions :

`ValueError : Returns a ValueError when floating                            point values are passed as parameters.TypeError : Returns a TypeError when anything other than                           numeric values are passed as parameters.`

Examples :

1. Program demonstrating the ValueError.

# imports random module
import random

'''If we pass floating point values as
parameters in the randint() function'''

r1 = random.randint(5.6511.30
print(r1)

Output :

2. Program demonstrating the TypeError.

# imports random
import random

'''If we pass string or character literals as
parameters in the randint() function'''

r2 = random.randint('a''z'
print(r2)

Output :

## ********END OF ARTICLE********

#### More Articles of Vishal Lodhi:

Name Views Likes
Python string zfill 114 0
Python string swapcase 92 0
Python string title 97 0
Python string startswith 103 0
Python string replace 144 0
Python string translate 85 0
Python string rpartition 86 0
Python string partition 74 0
Python string splitlines 114 0
Python string rsplit 79 0
Python string split 76 0
Python string rindex 82 0
Python string rfind 106 0
Python string upper 80 0
Python string lower 82 0
Python string maketrans 90 0
Python string strip 81 0
Python string rstrip 92 0
Python string lstrip 80 0
Python string rjust 97 0
Python string ljust 96 0
Python string len 82 0
Python string join 75 0
Python string casefold 82 0
Python string isprintable 85 0
Python string encode 78 0
Python string isdecimal 85 0
Python string isidentifier 78 0
Python string isupper 78 0
Python string istitle 80 0
Python string isspace 100 0
Python string isnumeric 102 0
Python string isdigit 83 0
Python string islower 84 0
Python string isalpha 96 0
Python string isalnum 102 0
Python string index 88 0
Python string find 94 0
Python string expandtabs() 81 0
Python string endswith 92 0
Python string count 86 0
Python string capitalize 78 0
Python string center 85 0
Python string Introduction 99 0
Python string Template 140 0
Python string Formatter 145 0
Python string printable 199 0
Python string whitespace 146 0
Python string punctuation 104 0
Python string octdigits 87 0
Python string hexdigits 104 0
Python string digits 80 0
Python string ascii_letters 344 0
Python string ascii_lowercase 147 0
Python string ascii_uppercase 156 0
Python random weibullvariate 164 0
python random paretovariate 134 0
Python random vonmisesvariate 178 0
Python random normalvariate 196 0
Python random lognormvariate 120 0
Python random gauss 120 0
Python random gammavariate 131 0
Python random expovariate 194 0
Python random betavariate 186 0
Python random triangular 117 0
Python random uniform 141 0
Python random random 117 0
Python random sample 183 0
Python random shuffle 179 0
Python random choices 234 0
Python random choice 166 0
Python random randint 149 0
Python random randrange 214 0
Python random getrandbits 141 0
Python random setstate 136 0
Python random getstate 178 0
Python random seed 168 1
Python random Introduction 175 0