Python random Module sample, Randint, shuffle, Choice stochastic functions

Source: Internet
Author: User
Tags shuffle

I. Introduction to the Random module

The random function in the Python standard library can generate randomly floating-point numbers, integers, strings, and even help you randomly select an element in a list sequence, disrupting a set of data, and so on.

Second, the random module important function

1) and random () returns the random real number n between 0<=n<1;
2), choice (seq) returns a random element from the sequence seq;
3), Getrandbits (n) returns n random bits in the form of a long integer;
4), Shuffle (seq[, random]) specify the SEQ sequence in situ;
5), sample (SEQ, n) select n Random and independent elements from the sequence seq;

Iii. description of the Random module method

The random.random () function is the most commonly used method in this module, and it generates a random floating-point number, ranging between 0.0~1.0.

random.uniform () just makes up for the insufficiency of the above function, it can set the range of floating-point number, one is the upper limit, one is the lower limit.

random.randint () randomly gives an integer int type, which can specify the range of this integer, as well as upper and lower values, Python random.randint.

Random.choice () can be returned from any sequence, such as a list, by selecting a random element, which can be used for strings, lists, tuples, and so on.

random.shuffle () This function method can be used if you want to shuffle an element in a sequence randomly.

random.sample () can randomly intercept fragments of a specified length from a specified sequence, without modifying them in situ.

Iv. Random Module Case Demo

Not to be continued.

V. Summary of the random module tutorial

Play Snake net Python home tip: Any random related problem in Python can be considered as the most basic requirement for programmers by first considering the random module and mastering the common methods.

Browse this article of netizens, are looking at:
100 Examples of Python exercises
Raspberry Pi Python Programming
Regular expressions
JSON tutorials
Apache Configuration
MySQL Database
Python tab page

Python random Module sample, Randint, shuffle, Choice stochastic functions

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.