Question No. 0001: Generate random numbers (by the way, the official documentation of the Random module)

Source: Internet
Author: User
Tags shuffle

Before I look at this, I'm going to take a look at the official documentation for the random module ...

In the entire random module, the most basic is random, which produces a floating-point number [0.0, 1.0]. All the functions under this module are actually bound to one called random. Ramdom The hidden instances of the class, of course you can instantiate this class yourself to get a new random number generator, and the random module also has a class called Systemrandom, which uses the system function os.urandom () Use random resources provided by the operating system to generate random numbers. (unintelligible ...) Only a part of the function that looks useful is selected ...

Ramdon.seed (A=none, version=2)

This is used to initialize the random number generator, if a is ignored (that is, the default value of None), then the current system time is the kind. (If the system provides a random resource then the game uses random resources instead of system time), and if A is an int, use it directly. When version is 2, str, bytes, ByteArray object is converted to an int and all of its bits are used, if version 1, then the value produced by the hash () of a is used.

Random.getrandbits (k)

Returns a K-bit random integer

Random.range (stop)/Random.randrange (Start, stop[, step])

Returns a randomly selected element from range (start, stop, Step), which in fact has the same effect as choice (start, stop, step), but does not actually produce a Range object ...

Random.randint (A, B)

Returns an integer of a <= N <= b equivalent to Randrange (A, b+1)

Random.choice (seq)

Randomly selects an element from a non-empty sequence seq and generates an INDEXERROR exception if the incoming sequence is empty:

Random.shuffle (x[, Random])

To randomly arrange X, the optional parameter is a function that requires 0 parameters, it should return a random floating-point number range of [0.0, 1.0), by default the function is random ().

It is important to note that this function changes the passed in list X itself, and the return value is None, so a reference must be passed in so that it does not make any sense: Random.shuffle ([1,2,3,4])

Random.sample (population, K)

Returns a list of length k, which selects K different elements from the sequence population to form the list, which is generally used for sampling ... (note here that the resulting list does not contain the same element, and if K is greater than Len (population), the error will be ...)

Some of the rest of the function feel is pure mathematics, also do not study ...

The source code is very simple ...

1 Importstring, Random2 3field = String.ascii_letters +string.digits4 defgetrandom ():5     return "". Join (Random.sample (field, 4))6 defgetrandomlist ():7     return "-". Join ([Getrandom () for_inchRange (4)])8 Print(Getrandomlist ())

Question No. 0001: Generate random numbers (by the way, the official documentation of the Random module)

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.