Python Random number and pythonrandom Random Number

Source: Internet
Author: User

Python Random number and pythonrandom Random Number

The random number generation function of Python is implemented in the random module, and various distributed pseudo-random number generators are implemented.


This module can generate a floating-point random number ranging from 0 to 1, or randomly select a random number in a sequence. The generated random number can be a uniform distribution, Gaussian distribution, and log normal distribution, negative Exponential Distribution and alpha and beta distribution. However, these random numbers are not suitable for encryption-oriented applications.


You can also derive a sub-class of the Random class and implement the functions random (), seed (), getstate (), and setstate () in the sub-classes, A new generator can provide a getrandbits () method, which allows randrange () to generate random numbers in any range.


Warning:

The random number in this module is a pseudo-random number and cannot be used for security encryption. If you need a real password-safe random number,You need to use the OS. urandom () or SystemRandom class in the random module to implement


Refer to the official documentation. Common functions of the random module are as follows:


1. random. random ()

Used to generate a random floating point number ranging from 0 to 1: 0 <= n <1.0


2. random. uniform (a, B)

Generates a random floating point number within a specified range. One of the two parameters is the upper limit and the other is the lower limit. If a> B, the random number n: a <= n <= B. If a <B, B <= n <=.

 

3. random. randint (a, B)

Generates an integer in the specified range. Where parameter a is the lower limit, parameter B is the upper limit, and the number of random integers generated n: a <= n <= B


4. random. choice (seq)

Obtain a random element from the sequence. The seqe parameter indicates an ordered type, which can be list, tuple, array, str, etc.


5. random. randrange ([start,] stop [, step])

Obtains a random number from a set that increments by the specified base number within the specified range. For example: random. randrange (10,100, 2), the result is equivalent to obtaining a random number from the [10, 12, 14, 16,... 96, 98] sequence. Random. randrange (10,100, 2) is equivalent to random. choice (range (10,100, 2) in the result.


6. random. shuffle (X[,Random])

Used to disrupt the elements in a list. You can use this function to write a shuffling program.

 

7. random. sample (seq, k)

The sample function does not modify the original sequence and returns an error if k is greater than the number of seq elements.


>>> random.random()                      # Random float x, 0.0 <= x < 1.00.37444887175646646>>> random.uniform(1, 10)                # Random float x, 1.0 <= x < 10.01.1800146073117523>>> random.randrange(10)                 # Integer from 0 to 97>>> random.randrange(0, 101, 2)          # Even integer from 0 to 10026>>> random.choice('abcdefghij')          # Single random element'c'>>> items = [1, 2, 3, 4, 5, 6, 7]>>> random.shuffle(items)>>> items[7, 3, 2, 5, 6, 4, 1]>>> random.sample([1, 2, 3, 4, 5],  3)   # Three samples without replacement[4, 1, 5]



 


Steps for generating random numbers in Python and the random number Quality

>>> Random. random () # Random float x, 0.0 <= x <1.00.37444887175646646> random. uniform (1, 10) # Random float x, 1.0 <= x <10.01.1800146073117523> random. randint (1, 10) # Integer from 1 to 10, endpoints included7 >>> random. randrange (0,101, 2) # Even integer from 0 to 10026 >>> random. choice ('abcdefghj') # Choose a random element 'C' >>> items = [1, 2, 3, 4, 5, 6, 7] >>> random. shuffle (items) >>> items [7, 3, 2, 5, 6, 4, 1] >>> random. sample ([1, 2, 3, 4, 5], 3) # Choose 3 elements [4, 1, 5] test the random number quality. Generally, the following tests are used:
Birthday spacings
Overlapping permutations
Ranks of matrices
Monkey tests
Count the 1 s
Parking lot test
Minimum distance test
Random spheres test
The squeeze test
Overlapping sums test
Runs test
The craps test


In python, how does one use random () to obtain a random number in the range [-)?

Import random
Random. uniform (-1, 1)
This is the random module.
Import random
Num =-1 + 2 * random. random ()
This is the random function.

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.