Examples of Python random number usage [Based on the random module], pythonrandom

Source: Internet
Author: User

Examples of Python random number usage [Based on the random module], pythonrandom

This example describes how to use a Python random number. We will share this with you for your reference. The details are as follows:

1. random. seed (int)

A seed value is given to a random number object to generate a random sequence.

For the input of the same seed value, the random number sequence is also generated.

Generally, the time, seconds, and other variable values are used as the seed values, so that the random series generated by each operation are different.

The parameter seed () is omitted, meaning that a random number is generated using the current system time.

Random. seed (10) print random. random () # 0.57140259469random.seed (10) print random. random () #0.57140259469 the same seed value produces the same random number as print random. random () # 0.428889054675random.seed () # The parameter is omitted, meaning that the current system time is print random. random () random. seed () print random. random ()

2. random. randint (a, B)

Returns a random integer in the specified range, including the upper and lower limits.

print random.randint(1,10)

3. random. uniform (u, sigma)

Random normal floating point number

print random.uniform(1,5)

4. random. randrange (start, stop, step)

A random number is randomly obtained in the upper and lower limit range by step.

print random.randrange(20,100,5)

5. random. random ()

Random floating point number

print random.random()

6. Randomly select characters

Randomly select n characters

print random.sample('abcdefghijk',3)

Select a random character

print random.choice('abcde./;[fgja13ds2d')

Randomly select several characters and splice them into new strings.

print string.join(random.sample('abcdefhjk',4)).replace(" ","")

7. random. shuffle

Randomly shuffles the list, that is, shuffling.

Shuffle only applies to the list. For Str, an error such as 'abcdef' is reported. For example, ['1', '2', '3', '5', '6 ', '7'] Yes

item=[1,2,3,4,5,6,7]print itemrandom.shuffle(item)print itemitem2=['1','2','3','5','6','7']print item2random.shuffle(item2)print item2

PS: Here are two related online tools for your reference:

Online random number/string generation tool:
Http://tools.jb51.net/aideddesign/suijishu

High-strength Password generator:
Http://tools.jb51.net/password/CreateStrongPassword

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.