Random function in python
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.
- 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()
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])
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)
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)
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)
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!!!