Difference between revisions of "Random module"

(Created page with 'The '''random module''' is used to generate pseudo-random numbers in Python. ==Useful Functions== The following are functions in the random module, see t…')
 
m (typo in spelling "them" lol)
 
Line 2: Line 2:
  
 
==Useful Functions==
 
==Useful Functions==
The following are functions in the random module, see the page on [[module (Python)|modules]] for directions on how to import thrm into your program.
+
The following are functions in the random module, see the page on [[module (Python)|modules]] for directions on how to import them into your program.
 
*<tt>random.'''random'''()</tt> returns a random [[floating point number]] between 0.0 and 1.0.
 
*<tt>random.'''random'''()</tt> returns a random [[floating point number]] between 0.0 and 1.0.
 
*<tt>random.'''randint'''(a,b)</tt> returns a random [[integer]] between a and b, inclusive.
 
*<tt>random.'''randint'''(a,b)</tt> returns a random [[integer]] between a and b, inclusive.

Latest revision as of 12:55, 30 June 2020

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