How to generate a random number using Python

Source: Internet
Author: User
This article describes how to generate a random number in Python. if you need a random number, you can refer to the relationship between the random number generated in Python and the most common functions in the random module, the following article describes the relationship between the random number generated by Python and the most commonly used functions in the random module. I hope you will get some benefits. The following is an introduction to this article.

Random. random () is used to generate

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, a random number is generated.

N: a <= n <= B. If
 
  

print random.uniform(10, 20)  print random.uniform(20, 10)  #---- #18.7356606526  #12.5798298022  random.randint 

Generates an integer in the specified range. Here, parameter a is the lower limit, parameter B is the upper limit, and Python generates random numbers.

Print random. randint (12, 20) # generated random number n: 12 <= n <= 20 print random. randint (20, 20) # The result is always 20 # print random. randint (20, 10) # This statement is incorrect.

The lower limit must be less than the upper limit.

Random. randrange

This article describes the application that generates a random number in python from a set that increments by a specified base number within a specified range.

Random integer:
>>> Import random
>>> Random. randint (0, 99)
21

Randomly select an even number between 0 and 100:
>>> Import random
>>> Random. randrange (0,101, 2)
42

Random floating point number:
>>> Import random
>>> Random. random ()
0.85415370477785668
>>> Random. uniform (1, 10)
5.4221167969800881

Random characters:
>>> Import random
>>> Random. choice ('abcdefg & # % ^ * f ')
'D'

Select a specific number of characters from multiple characters:
>>> Import random
Random. sample ('abcdefghj', 3)
['A', 'D', 'B']

Select a specific number of characters to form a new string:
>>> Import random
>>> Import string
>>> String. join (random. sample (['A', 'B', 'C', 'D', 'e', 'e', 'F', 'G', 'H', 'I ', 'j'], 3 )). r
Eplace ("","")
'Fih'

Randomly selected string:
>>> Import random
>>> Random. choice (['apple', 'pear ', 'peach', 'Orange ', 'lemon'])
'Limon'

Shuffling:
>>> Import random
>>> Items = [1, 2, 3, 4, 5, 6]
>>> Random. shuffle (items)
>>> Items
[3, 2, 5, 6, 4, 1]

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.