Python random function random random access to numbers strings lists etc.

  • 2021-10-24 23:11:52
  • OfStack

The module used to generate random numbers in python is random, and import is needed before use. Let's look at its usage below.

Python randomly generates a floating point number random. random

random. random () is used to generate 1 random character number from 0 to 1: 0 < = n < 1.0

Note: The following code passed the test under Python 3.5, and the python 2 version can be slightly modified

Describe

The random () method returns a randomly generated real number in the range (0, 1).

Grammar

Here is the syntax for the random () method:

import random

random.random()

Note: random () is not directly accessible. You need to import the random module and then call the method through the random static object.

Parameter

None

Return value

Returns a randomly generated real number in the range [0, 1].

Instances

The following shows an example using the random () method:


>>> import random
>>> print ("random 1 : ", random.random())
random 1 : 0.8325656436327418
>>> print ("random 2 : ", random.random())
random 2 : 0.9205070325517919

The output result of the above example after running is:

random 1 : 0.8325656436327418

random 2 : 0.9205070325517919

Python randomly generates a floating-point number random. uniform with specified precision

The function prototype of random. uniform is random. uniform (a, b), which is used to generate a random symbol number in a specified range, with two parameters, one of which is the upper limit and one is the lower limit. If a > b, the generated random number n: b < = n < = a. If a < b, then a < = n < = b.


>>> import random
>>> print (random.uniform(1, 10))
2.444412473072692
>>> print (random.uniform(10, 1))
4.617930290730046

Results:

2.444412473072692

4.617930290730046

Python randomly generates a real number random. randint

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

Note: The lower limit must be less than the upper limit


>>> import random
>>> print (random.randint(11, 20)) # Generated random number n: 12 <= n <= 20
13
>>> print (random.randint(20, 20)) # The result will always be 20
20

Results:

13

20

Python Gets the random number within the specified range random. randrange

The randrange () method returns one random number from the specified incremented cardinality set, with a cardinality default of 1.

The function prototype of random. randrange is random. randrange ([start], stop [, step]), which takes a random number from a set incrementing by the specified cardinality in a specified range.

For example, random. randrange (10, 100, 2), the result is equivalent to obtaining a random number from the sequence [10, 12, 14, 16,... 96, 98].

random. randrange (10, 100, 2) is equivalent to random. choice (range (10, 100, 2).


>>> import random
>>> print (random.randrange(10, 18, 2))
16

Results:

16

Python Randomly gets child elements in a list, tuple, or string random. choice

random. choice fetches a random element from a sequence.

Its function prototype is random. choice (sequence). The parameter sequence represents an ordered type.

Here, I want to explain 1: sequence is not a specific type in python, but generally refers to the type of 1 series. list, tuple, and strings all belong to sequence. For sequence, see Chapter 1 of the python Manual Data Model


>>> import random
>>> print (random.choice("ofstack.com"))
t
>>> print (random.choice(["jb", "51", "net"]))
jb
>>> print (random.choice(("jb", "51", "net")))
51

Results:

t

jb

51

Python List Random Sort random. shuffle

The function prototype of random. shuffle is random. shuffle (x [, random]), which is used to scramble the elements in a list. Such as:


>>> import random
>>> list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> random.shuffle(list)
>>> print (list)
[7, 2, 8, 5, 3, 10, 1, 9, 6, 4]

Results:

[7, 2, 8, 5, 3, 10, 1, 9, 6, 4]

Python Randomly fetches a specified number of items in the list random. sample

The function prototype of random. sample is random. sample (sequence, k), which randomly obtains fragments of a specified length from a specified sequence.

The sample function does not modify the original sequence.


>>> import random
>>> list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> slice = random.sample(list, 5) # From list Random acquisition in 5 Elements, as 1 Segment returns 
>>> print (slice)
[1, 5, 9, 3, 2]
>>> print (list) # The original sequence will not change. 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Results:

[1, 5, 9, 3, 2]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

This article mainly explains the use of Python random function random, including Python random numbers, Python random string, Python random list, etc. For more information about the use of Python random function random, please click the relevant links below


Related articles: