In addition to using the familiar Numpy. Random module to generate random numbers, or random sampling, in data analysis, data cleaning, and data set processing, the Python standard library also provides the Random module. The Random module provided by the standard library is a lightweight alternative and works almost identically.

1. Import modules

Random is the Python standard library module. It is installed with Python and can be imported directly.

import random
Copy the code

2. Practical methods

2.1 the random ()

Random () generates a floating point number in a semi-open interval [0, 1], and almost all random module methods depend on Random () for implementation.

random.random()
Copy the code
0.9111245252327139
Copy the code

2.2 randint (a, b)

Returns a random integer N such that a <= N <= b. That’s the randrange of a, b plus 1.

random.randint(0.10)
Copy the code
8
Copy the code

2.3 randrange (start, stop, [step])

Returns a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, Stop, step)), but does not actually build a range object.

random.randrange(0.10.2)
Copy the code
2
Copy the code

2.4 choice (seq)

Returns a random element from the non-empty sequence seq. If seq is empty, IndexError is raised.

random.choice([0.1.2.3.4.5])
Copy the code
2
Copy the code

2.5 choices(population, weights=None, *, cum_weights=None, k=1)

Select replace from Population to return the list of elements of size K. If population is empty, raise IndexError.

If a weight sequence is specified, the selection is based on the relative weight. Alternatively, if a cum_weights sequence is given, the selection is based on the cumulative weights (possibly computed using itertools.accumulate()). For example, relative weights [10, 5, 30, 5] are equivalent to cumulative weights [10, 15, 45, 50]. Internally, relative weights are converted to cumulative weights before selection, so providing cumulative weights saves work.

random.choices([0.1.2.3.4.5], k=2)
Copy the code
[0, 2)Copy the code
random.choices([0.1.2.3.4.5], cum_weights=[10.20.30.40.50.60], k=3)
Copy the code
[4, 4, 4]
Copy the code

2.6 shuffle (x [, random])

Shuffle the sequence X randomly.

The optional argument random is a zero-argument function that returns a random floating point number in [0.0, 1.0); by default, this is the function random().

To change an immutable sequence and return a new scrambled list, use sample(x, k=len(x)).

a = [0.1.2.3.4.5]
random.shuffle(a)
a
Copy the code
[5, 2, 3, 0, 1, 4]
Copy the code

2.7 the sample (population, k)

Returns a k-length list of unique elements selected from the population sequence or collection. Used for random sampling without duplication.

random.sample([1.2.3.4.5.6], k=2)
Copy the code
[1, 3]
Copy the code

2.8 gauss (mu, sigma)

Gaussian distribution. Mu is the mean, sigma is the standard deviation.

[random.gauss(0.1) for i in range(10)]
Copy the code
[1.232295558291998,
 -0.23589397085653746,
 -1.4190307151921895,
 0.18999908858301912,
 0.780671045104774,
 0.041722424850158674,
 0.7392269754813698,
 1.4612049925568829,
 0.09647538110312114,
 -0.32525720572670025]
Copy the code

3. Brief source code

The following is the source code for the Random module on Github to help you understand the basic structure of the Random module and the source code statement for the utility methods described in this article.

random.py:

class Random(_random.Random):
    def seed(self, a=None, version=2):
        pass
    def randrange(self, start, stop=None, step=1, _int=int):
        pass
    def randint(self, a, b):
        pass
    def choice(self, seq):
        pass
    def shuffle(self, x, random=None):
        pass
    def sample(self, population, k):
        pass
    def choices(self, population, weights=None, *, cum_weights=None, k=1):
        pass
    def gauss(self, mu, sigma):
        pass. _inst = Random() seed = _inst.seed random = _inst.random randrange = _inst.randrange randint = _inst.randint choice = _inst.choice shuffle = _inst.shuffle sample = _inst.sample choices = _inst.choices gauss = _inst.gauss ...Copy the code

reference

  • [1] Python docs: Random — Generate pseudo-random numbers
  • [2] github: python/cpython/Lib/random.py

Writing a column is not easy, so if you find this article helpful, give it a thumbs up. Thanks for your support!

  • Personal website: Kenblog.top
  • Github site: kenblikylee.github. IO



Wechat scan qr code to obtain the latest technology original