5 Helpful Python Random Module Methods

Hello, I’m Aya Bouchiha, today, I’m going to share with you 5 useful & helpful methods from a random module.

Firstly, we need to know that the Random module is built-in in python for helping you to create random numbers.

random()

rando…


This content originally appeared on DEV Community and was authored by Aya Bouchiha

Hello, I'm Aya Bouchiha, today, I'm going to share with you 5 useful & helpful methods from a random module.

Firstly, we need to know that the Random module is built-in in python for helping you to create random numbers.

random()

random(): returns a random float n where 0 <= n < 1

import random

print(random.random()) # 0.7291047713945417

randint()

randint(a, b): returns a random integer between the given range. a <= n <= b

import random

print(random.randint(1,  10)) # 7
print(random.randint(-12, 2)) # -10

uniform()

uniform(a, b): returns a random float between the given range. a <= n <= b

import random

print(random.uniform(5.7, 12)) # 10.096664083501162
print(random.uniform(10, 100.2)) # 95.00994365426938

shuffle()

shuffle(sequence, func = random.random): this method shuffle the giving sequence.In addition, It updates the giving sequence and does not return a new one.

import random

users = ['aya', 'simon', 'john']
random.shuffle(users)
print(users) # ['john', 'aya', 'simon']

choice()

choice(sequence): returns a random element from the giving sequence.

import random

users = ['aya', 'john', 'simon', 'kyle']
winner = random.choice(users)
print(winner) # aya :)

Summary

  • random(): returns a random float n where 0 <= n < 1.
  • randint(a, b): returns a random integer between the given range. a <= n <= b.
  • uniform(a, b): returns a random float between the given range. a <= n <= b.
  • shuffle(sequence, func = random.random): this method shuffle the giving sequence.
  • choice(sequence): returns a random element from the giving sequence.

References & useful Ressources

Happy coding!


This content originally appeared on DEV Community and was authored by Aya Bouchiha


Print Share Comment Cite Upload Translate Updates
APA

Aya Bouchiha | Sciencx (2021-07-24T23:29:58+00:00) 5 Helpful Python Random Module Methods. Retrieved from https://www.scien.cx/2021/07/24/5-helpful-python-random-module-methods/

MLA
" » 5 Helpful Python Random Module Methods." Aya Bouchiha | Sciencx - Saturday July 24, 2021, https://www.scien.cx/2021/07/24/5-helpful-python-random-module-methods/
HARVARD
Aya Bouchiha | Sciencx Saturday July 24, 2021 » 5 Helpful Python Random Module Methods., viewed ,<https://www.scien.cx/2021/07/24/5-helpful-python-random-module-methods/>
VANCOUVER
Aya Bouchiha | Sciencx - » 5 Helpful Python Random Module Methods. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/24/5-helpful-python-random-module-methods/
CHICAGO
" » 5 Helpful Python Random Module Methods." Aya Bouchiha | Sciencx - Accessed . https://www.scien.cx/2021/07/24/5-helpful-python-random-module-methods/
IEEE
" » 5 Helpful Python Random Module Methods." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/07/24/5-helpful-python-random-module-methods/. [Accessed: ]
rf:citation
» 5 Helpful Python Random Module Methods | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/07/24/5-helpful-python-random-module-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.