Python generates random function randomness

Source: Internet
Author: User
Tags shuffle

Random is a built-in (built-in) function that generates random numbers

Import module:

Import

 

You can then invoke the function under the random module using dir (random) to see what functions are under the random module, with the following results:

1>>>dir (random)2['BPF','LOG4','Nv_magicconst','RECIP_BPF','Random','Sg_magicconst','Systemrandom','Twopi','_builtinmethodtype','_methodtype','_sequence3 ', '_set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect4 ', '_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom'  5,'_warn','betavariate','Choice','Choices','expovariate','gammavariate','Gauss','getrandbits','GetState','lognormvariate','normalvariate',   6 'paretovariate','Randint','Random','Randrange','Sample','seed','SetState','Shuffle','Triangular','Uniform','vonmisesvariate','Weibullvaria7Te'] 

The most commonly used functions are as follows:

Random.randint

1 random.randint (1,10)

The meaning of the statement is to produce a random number (integer int) of 1 to 10 (containing 1 and 10). (The parameter is an integer cannot be a floating-point number otherwise it will be an error)

1 # the statement is incorrect. Lower limit must be less than or equal to upper limit 

Random.random

1

Generate a random floating-point number from 0 to 1, including 0 but not 1, that is, [0.0, 1.0].

Random.uniform

1 random.uniform (A, B)

Generates a random floating-point number between A and B. Unlike Randint, however, A and B can be either integers or sizes.

That

1  Random.uniform (3.65,10.56)# can do this
1 random.uniform (10.56, 3.65)# You can do   the same

  

Random.choice

1 random.choice (seq)

Randomly selects an element from a sequence. Seq needs to be a sequence, such as a list, a tuple, a string.

1Random.choice ([1, 2, 3, 5, 8, 13])#List2   3Random.choice ('Hello')#string4   5Random.choice (['Hello',' World'])#string consisting of a list6   7Random.choice ((1, 2, 3))#Meta-group

is a viable use.

Random.randrange

1 random.randrange (start, stop, step)

Generate a random integer from start to stop (not including stop), at intervals of step. Start, stop, step are all integers, and start<stop. Such as:

1  Random.randrange (0, 2)#  randomly generates an even number between 0-20

Random.sample

1

From the P sequence, a random fetch of k elements is generated, generating a new sequence. Sample does not change the original sequence.

This module is 666, and supports very specialized stochastic algorithms such as triangulation, β distribution, exponential distribution, gamma distribution, Gaussian distribution, and so on.

Random.shuffle

1 random.shuffle (x)  

Disrupts the order of elements in sequence x. Shuffle directly changes the original sequence. Such as:

1 Import Random   2 a=[1,2,3,4,5,6]  3random.shuffle (a)  4print(a

The results are as follows:

1 [5, 1, 3, 6, 4, 2]

Novice there may be some errors when using this function, as follows:

1 Import Random   2 a=[1,2,3,4,5,6]  3print(Random.shuffle (a))  

Using this method results in a none because Random.shuffle () is used to disrupt the list element, there is no return value, so print (Random.shuffle (a)) cannot be used to output the printing scrambling

After the sequence

Python generates random function randomness

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.