Python random module, pythonrandom Module
Copy codeThe Code is as follows:
Import random
Print random. random ()
Obtains a floating point number smaller than 1.
Copy codeThe Code is as follows:
Import random
Random. randint (1, 10)
Returns an integer from 1 to 10.
Copy codeThe Code is as follows:
Import random
Print random. uniform (0, 2)
Obtains a floating point number greater than 0 and less than 2.
Copy codeThe Code is as follows:
Import random
Print random. randrange (1, 10, 4)
Obtain a random number ranging from 1 to 10 in step 4.
Copy codeThe Code is as follows:
Import random
A = [1, 2, 3, 4, 5]
Random. choice ()
Extracts an element from list a at random.
Copy codeThe Code is as follows:
Import random
A = [1, 2, 3, 4, 5]
Random. shuffle ()
Disrupt the order of elements in List
Copy codeThe Code is as follows:
Import random
A = [1, 2, 3, 4, 5]
Random. sample (a, 3)
Three elements can be retrieved from list a in a random order (one element can only be retrieved once, so the number of retrieved elements cannot be greater than the number of elements contained in the List)
What is the difference between randomrandom () and randomRandom () in python?
Python is completely object-oriented, so all data is objects
Random. random () generates a random floating point float between 0 and 1. It is actually the Random method of a hidden random. random class instance.
Random. Random () generates an instance of the random class in the Random module. This instance will not share the state with other Random instances, which is generally used in the case of multithreading.
Common installation methods for Python modules
I. Method 1: single file module
Directly copy the file to $ python_dir/Lib.
Ii. Method 2: Multi-file module with setup. py
Download the module package, decompress it, enter the module folder, and execute:
Python setup. py install
3. Method 3: easy_install
Download ez_setup.py and run python ez_setup to install the easy_install tool. Then you can use easy_install to install the package.
Easy_install packageName
Easy_install package. egg
Iv. Method 4: pip
Install the pip tool: easy_install pip (pip can be installed through easy_install and installed in the Scripts folder .)
Install: pip install PackageName
Update: pip install-U PackageName
Remove: pip uninstall PackageName
Search: pip search PackageName
Help: pip help