# How To Generate Random Number In Python?

Random Numbers are an important part of the programming languages. Random numbers are used for randomness, security, lottery-like applications.

## What Is Random, True Random, Pseudorandom?

Even it may seem a simple subject random numbers are a fundamental topic of security. Randomly created numbers are used to create certificates, passwords, passphrases, authentication, or even identification. Even most of the libraries provide random number generator or methods they are not completely random. They are mostly created via CPU or some nonperiodic actions like user input, network traffic, etc. True random means completely non-periodic and can not be predicted, anyone. Pseudorandom is a term used to describe different random functions provided via programming languages and libraries. Pseudorandom is not truly random but provides similar attributes to it and can be used in most of the cases.

## Seed The Randomness with seed() Method

seed() method is used to initialize the random number generator. Even it is not mandatory if not initialized the system clock is used as the randomness source for the random number generating. If enabled with the seed(1) the system provided random number generator device like /dev/urandom etc. is used which is more reliable.

``random.seed(1)``

## Generate Random Number with randint() Method

Random numbers can be generated by using different methods provided by Python but the most popular and straightforward method is randint() method. This method can be used to generate random numbers at the specified range where we will provide a start and stop number and the random number will be generated between these start and stop numbers. randint() method only accepts integers for start and stop parameters and returns the only integer random numbers.

``randint(start,stop)``
• start is the start number for random number generation. Generated random number can not be less than start.
• stop is the end number for random number generation. Generated random number can not be bigger than stop.
``````import random

random.randint(1,2)
// 1

random.randint(1,2)
// 1

random.randint(1,3
)
// 2

random.randint(1,3)
// 1

random.randint(1,3)
// 2

random.randint(1,4)
// 4

random.randint(1,4)
// 4

random.randint(1,4)
// 4

random.randint(1,4)
// 2

random.randint(1,400)
// 49

random.randint(1,400)
// 250

random.randint(1,400)
// 15

random.randint(1,400)
// 200``````

## Generate Random Number with randrange() Method

randrange() is a similar method to the randint() which generates random integers. the only difference is a step that can be specified for random number generation where generated random number will be one of these steps.

``random.randrange(start,stop,step)``
• start parameter specifies the start of the steps.
• stop parameter specified at the end of the steps.
• step is the increasing number or iteration like if the step is 3 and start is 6 the possible numbers to be selected will be 6,9,12,15 etc.

In the following example, we will start from 3 and end 21 by using the 3 as steps.

``random.randrange(3,21,3)``

There is also an alternative usage of the randrange() method where we can only provide the end number like below. The start number will be 0 in this case.

``random.randrange(10)``

## Generate Random Number with uniform() Method

uniform() method is another method used to generate random numbers but in floating-point type. It accepts two parameters to set the estart and end of the range.

``random.uniform(start,end)``
• start parameter will set start of the range.
• end parameter will set end of the range.
``random.uniform(0,10)``

## Generate Random Number with NumPy randint() Method

NumPy is a popular Python library that provides numerical and calculation related modules and methods. NumPy also provides random number generation methods. randint() is a method that can generate random numbers and matrices.

``numpy.random.randint(start,end,size)``
• start parameter sets the start number of the matrice elements.
• end parameter sets the end number of the matrice elements.
• size parameter specifies the matrice dimensions.

As a result, this method will return a matrice with 3 dimensions.

``````import numpy

randomArray = numpy.random.randint(10,50,size=(3,3))``````

## Shuffle Randomly Given List with shuffle() Method

shuffle() is another method provided with the random module. This method will shuffle the given sequence type which can be a list, tuple, etc. There is a limit for shuffle operation where the sequence can not contain more than 2048 elements. The shuffled sequence will be stored inside the given sequence variable.

``````l = [1,2,3,4,5,6,7,8]

random.shuffle(l)``````