Python -- random Number Generation

Source: Internet
Author: User

Python -- random Number Generation

Http://blog.csdn.net/jgood/article/details/4278885

The random module in Python is used to generate random numbers. The following describes the most common functions in the random module.

Random. random

Random. random () is used to generate a random number of points 0 to 1: 0 <= n <1.0

Random. uniform

The prototype of the random. uniform function is random. uniform (a, B), which is used to generate a random number of characters in 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: B <= n <=. If

[Python]View plaincopy
  1. Print random. uniform (10, 20)
  2. Print random. uniform (20, 10)
  3. # ---- Results (results on different machines are different)
  4. #18.7356606526
  5. #12.5798298022

    Random. randint

    The prototype of the random. randint () function is random. randint (a, B), which is used to generate an integer in the specified range. Where parameter a is the lower limit, parameter B is the upper limit, and the generated random number n: a <= n <= B

    [Python]View plaincopy
    1. Print random. randint (12, 20) # generated random number n: 12 <= n <= 20
    2. Print random. randint (20, 20) # The result is always 20
    3. # Print random. randint (20, 10) # This statement is incorrect. The lower limit must be less than the upper limit.

      Random. randrange

      The prototype of the random. randrange function is random. randrange ([start], stop [, step]). a random number is obtained from the 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.

      Random. choice

      Random. choice gets a random element from the sequence. The function prototype is random. choice (sequence ). The sequence parameter indicates an ordered type. Here you wantDescriptionBelow: sequence is not a specific type in python, but a series of types. List, tuple, and string all belong to sequence. For sequence, you can view the python manual data model chapter or refer to: http://www.17xie.com/read-37422.html. The following are examples of using choice:

      [Python]View plaincopy
      1. Print random. choice ("Learning Python ")
      2. Print random. choice (["JGood", "is", "a", "handsome", "boy"])
      3. Print random. choice ("Tuple", "List", "Dict "))

        Random. shuffle

        The original function of random. shuffle is random. shuffle (x [, random]), which is used to disrupt elements in a list. For example:

        [Python]View plaincopy
        1. P = ["Python", "is", "powerful", "simple", "and so on..."]
        2. Random. shuffle (p)
        3. Print p
        4. # ---- Result (the results on different machines may be different .)
        5. # ['Powerful', 'simple', 'is', 'python', 'and so on... ']

          Random. sample

          The prototype of the random. sample function is random. sample (sequence, k). Fragments of the specified length are randomly obtained from the specified sequence. The sample function does not modify the original sequence.

          [Python]View plaincopy
          1. List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
          2. Slice = random. sample (list, 5) # randomly obtain 5 elements from the list and return them as a piece.
          3. Print slice
          4. Print list # The original sequence has not changed.

            These methods are the most commonly used in the random module. In the Python manual, we also introduce other methods. If you are interested, you can query the Python manual for more details.


            Other functions in numpy:

            Linspace (start, end, num): for example, the result of linspace (0.1, 11) is [0, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];

            Arange (n): generates a vector from 0 to n-1. For example, arange (4) Returns [,].

            Random. random ([...]):Generate random matrixFor example, random. random ([2, 3]) generates a random number of 2x3 dimensions.



            From:

            Http://blog.csdn.net/jgood/article/details/4278885

            Ref:

            The Python Standard Library

            Use matrix \ special functions in Numpy


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.