Difference between revisions of "Random module"

m (typo in spelling "them" lol)
 
Line 13: Line 13:
 
*[http://en.wikipedia.org/wiki/Pseudorandom_number_generator Pseudorandom Number Generators on Wikipedia]
 
*[http://en.wikipedia.org/wiki/Pseudorandom_number_generator Pseudorandom Number Generators on Wikipedia]
  
[[Category: Introduction to Programming]]
+
[[Category:Introduction to Programming]]
 +
 
 
[[Category:Python Modules]]
 
[[Category:Python Modules]]
 +
 +
[[Category:Computer programming]]

Latest revision as of 15:52, 18 May 2025

The random module is used to generate pseudo-random numbers in Python.

Useful Functions

The following are functions in the random module, see the page on modules for directions on how to import them into your program.

  • random.random() returns a random floating point number between 0.0 and 1.0.
  • random.randint(a,b) returns a random integer between a and b, inclusive.
  • random.uniform(a,b) returns a random floating point number between a and b.
  • random.choice(s) returns a random element of sequence s. For example, random.choice([1,3,7]) will return either 1, 3, or 7.
  • random.shuffle(s) shuffles sequence s in place. Hence, the original s is changed and nothing is returned. If you want to preserve the original order also you need to copy the list first (myListCopy = myList[:] would work).

See Also