Python's Random module function analysis (i)

Source: Internet
Author: User
Tags random seed shuffle


Random is the module in which Python generates pseudo-random numbers, and the stochastic seed defaults to the system clock. The methods in the following analysis module:

1.random.randint (start,stop):

This is a function of generating an integer random number, the parameter start represents the minimum value, the parameter stop represents the maximum value, both ends of the value can be taken;

Time complexity of the function algorithm: O (1)
Core Source code:
Return Self.randrange (A, b+1) # is encapsulated by the Randrange function
Example:

1  for  in range:2print(rm.randint (0, ten), end=")   

2.random.randrange (Start,stop,step):
is also a random integer function, the parameters are optional;

when there is only one parameter, the default random range of 0 to the parameters, front closed after open, two parameters, Min and Max, front closed after open; three parameters, minimum, maximum and step, front closed and open.

Algorithm time Complexity: O (1)
Core Source code:
return Istart + istep*self._randbelow (n) # This function is encapsulated by the _randbelow function
Example:

1  for  in range:2print#  generates 0 to 10 (not included) random number 3Print  #  generate 5 to 10 (not included) random number 4print#  Generates a random number of multiples of 5 in the range 5 to 100 (not included)         

3.choice (seq):

a random selection function, SEQ is a non-empty collection, randomly selects an element output in the collection, there is no restriction on the type of the element.
Core source code:
i = Self._randbelow (Len ( SEQ)    # random subscript
return seq[i]
time complexity: O (1)
example:

1 list3 = ["wo"" I am ", 2, 8, [2, 3]]2  for  in range:3print(Rm.choice (LIST3), end="")   

4.random ():

This function forms any floating-point number from 0.0 to 1.0, left closed right, without parameters.
Example:

1  for  in range:2print(Rm.random (), end="")   

5.send (N=none):

a function that initializes the random number generator, n represents a random seed, and when N=none, the random seed is the system time, when n is other data, such as INT,STR, and so on, the supplied data is used as a random seed, at which time the generated random sequence is fixed.

Example:

1 rm.seed ("hdsfsf")2for in# No matter how many times the program starts, the output sequence does not change 3 Print (Rm.randint (0, ten), end=")   

6.getstate () and SetState (state):

The GetState () function is used to record the state of the random number generator, and the SetState (state) function is used to restore the generator to its last record.
Example:

1Tuple1 = Rm.getstate ()#record the state of the generator2  forIinchRange (20):3     Print(Rm.randint (0, ten), end=' ')4 Print()5Rm.setstate (Tuple1)#state before recovery after passing in parameters6  forJinchRange (20):7     Print(Rm.randint (0, ten), end=' ')#the results of the two sets of outputs are the same

7.shuffle (Seq,random=none):

The incoming collection is manipulated in a random order. Only for variable sequences, such as strings, lists, for tuples and other immutable sequences will be error, random used to choose the way of disorderly operation, such as: Random=random.
Core Source code:

 for  in Reversed (range (1= Randbelow (i+1= X[j], x[i]


Time complexity: O (N)
Example:

1 list3 = ["wo"" I am ", 2, 8, [2, 3]]2 Print (LIST3) 3 rm.shuffle (list3, random=None)4print(LIST3)

8.sample (population, K):

The population parameter is a sequence, such as a list, a tuple, a collection, a string, and so on, randomly extracting k elements from a collection to form a new sequence without altering the original sequence.

Core Source code:

 for inch  #  Use the Randbelow function to get a random integer while in# to get the random number de-weight j =   #  Assign Value


Worst time complexity: O (N*n)
Example:

1 list3 = ["wo"" I am ", 2, 8, [2, 3], 2, 2, 8]2  Print(list3)3 list1 = Rm.sample (List3, 4)4Print (List1)

9.uniform (A, B):

A function that generates a floating-point number between parameters A through B, and if a > b, generates a floating-point number between B and a.
Core Source:
Return a + (b-a) * Self.random () # An encapsulation of the random function
Time complexity: O (1)
Example:

1  for  in range:2print(Rm.uniform (10, 1))   

Python's Random module function analysis (i)

Related Article

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.