Random function in python

Nitesh Pandey
Analytics Vidhya
Published in
3 min readOct 16, 2019
Photo by Chris Ried on Unsplash

There is a lot of modules available in python but one of them is a Random. Random module is used to generate a pseudo-random number.Random module consists of different built-in functions but we learn some of the most widely used Random functions in this post.

  1. random()

Generate pseudo-random float numbers, random() method returns the floating-point number that is in the range of [0, 1) but not including 1 and 0.
It does not take any positional argument.

Syntax-random.random()

random.random() method return the floating point number in the range of 0 to 1
random.random()

2. randrang()

It chooses an integer and defines the range in-between,It will return a<=value< b, It can take more than one argument.

Syntax-random.randrange(start, stop [, step])

Generate random number using randrange() method
Random Range function with one argument
different scenario for randomrange() method
Random Range function with more than one argument

3. random.randint()

It generates the random number within the specified limit. list object can not be used in randint(), It takes two positional arguments.it is used when we know the start and end values.It will return a<=value<= b

Syntax-random.randint(a,b)

An error occurred when we used list object
random.randint()

4. random.uniform()

uniform() generates a random float number which is greater than or equal to the start number and less than or equal to the stop number. It also takes two positional arguments.

Syntax-random.uniform(Start, Stop)

random.uniform()

5. random.choice()

When we want to select an item at random in a given range or a list or something that is iterable then we use this function. choice function introduced in python version 3.6 and it can repeat the elements.

Syntex -random.choice(ourchoice)

random.choice()

I think you enjoyed this post if you find some great strategies of your own related to this post then let us known in the comments. Thank you!

Enjoy Learning!!!

--

--

Nitesh Pandey
Analytics Vidhya

Software Developer, Enthusiastic to learn new technologies in the field of Artificial Intelligence, Owner of technocutter.com,cracklogic.com and more sites.