Python random module, pythonrandom Module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.