Python3 learning: a random number generator and random function

Python random number generation and random function

This article will Python3 the many uses of random functions are introduced briefly.

1. random.random()

Role: Returns a range 0 , 1 0,1 random number in the.

[Example]

    import random
    print("random number is:",random.random())

2. random.uniform()

Action: generating a random number within a specified range.

Syntax: random.uniform (lower bound, upper bound)

[Example]

import random
print("random number is:",random.uniform(114,514))

3. random.randint ()

Action: generating a random integer within the specified range.

Syntax: random.randint (lower bound, upper bound)

[Example]

import random
print("random integer is:",random.randint(114,514))

4. random.randrange ()

Action: Given the initial value, and the tolerance upper bound for the initial value as the lower bound, a random configuration of an arithmetic sequence is within the given range.

Syntax: random.randrange (initial value, the upper bound tolerance)

[Example]

import random
print(random.randrange(114,1919,810))

5. random.choice()

Action: acquiring a random element from the given string, array, etc. "Sequence".

Syntax: random.choice (sequence)

[Example]

import random
print(random.choice(1145141919))

6. random.shuffle()

Role: upset the given sequence.

Syntax: random.shuffle (sequence)

[Example]

import random
print(random.shuffle(1145141919))

7. random.sample()

Effect: specific length fragment randomly extracted from a specified sequence, does not change the original sequence.

Syntax: random.sample (sequence, segment length)

[Example]

import random
print(random.sample(1145141919,8))
Published 32 original articles · won praise 14 · views 6362

Guess you like

Origin blog.csdn.net/u010186354/article/details/104112368