The use of Python notes random library

1. Random library call
import random
or
from random import *
 
2. Random library commonly used function
function description
seed () initialization random number seed, the default value is the current system time
random () randomly generates a decimal between [0.0, 1.0)
randint (a, b) randomly generates an integer in [a, b]
randrange (a, b, c) randomly generates an integer
uniform (a, b) between [a, b) with a step size of c Randomly generate a random decimal
choice ( [a, b] ) from the sequence type (such as a list) to randomly return an element
shuffle () to shuffle the elements in the sequence and return
2. Experiment
>>> random ()
0.8212324692623353
>>> randint (1,10)
9
>>> randrange (1,10,2)
3
>>> uniform (1,5)
2.5155496299589983
>>> choice (range (20))
2
>>> s = (1 , 5,6,9,8,7]
>>> shuffle (s)
>>> print (s)
[1, 5, 9, 8, 7,6]
>>> choice(s)
9
 
2.2 Application of seed () function
Before generating a random number, you can first specify a random number seed through the seed () function. The random number seed is generally an integer. As long as the seed is the same, the random number sequence generated each time is also the same

2.2. Example
>>> "{}". Format (randint (1,10))
'10'
>>> "{}". Format (randint (1,10))
'1'
>>> seed (10)
>>> "{}". format (randint (1,10))
'10'
>>> "{}". format (randint (1,10))
'1'
 
We assigned the seed twice, the first The results of the first and second times are exactly the same, so we can know the role of random number seeds.

2. Expanded
random numbers and random events are uncertain, and their results are unpredictable and unpredictable before they are generated. However, the random number generated by the computer is predictable and determinable, because the random number generated by the computer is the product of the algorithm (Mersenne Twister algorithm), so the result generated by the computer is called "pseudo random number" It can be seen from the seed () function above that the random library generates a pseudo-random number sequence each time.

3 Guess the game
Q: Please preset an integer between 1 ~ 10, let the user make a guess, if it is greater than the preset number, it will display a too large prompt, if it is less than the preset number, it will pop up too small prompt. Repeat in this way until you guess the preset number.

from random import *
num = randint (1,10) tnum
= eval (input ("Please guess an integer between 0 ~ 9:"))
while num! = tnum:
if num <tnum:
print ("Big" )
tnum = eval (input ("Please guess again an integer between 0 ~ 9:"))
elif num> tnum:
print ("Small")
tnum = eval (input ("Please guess again between 0 ~ 9 An integer between: "))
else:
print (" You guessed it ")

 
Add a question: If you want to display the total number of guesses at the end, how do you need to modify the program?

from random import *
num = randint (1,10)
count = 1
tnum = eval (input ("Please guess an integer between 0 ~ 9:"))
while num! = tnum:
count + = 1
if num < tnum:
print ("Big")
tnum = eval (input ("Please guess again an integer between 0 ~ 9:"))
elif num> tnum:
print ("Small")
tnum = eval (input ("Please Again guess an integer between 0 ~ 9: "))
else:
print (" You guessed it right ")
print (" You guessed {} times ".format (count))

 
4. The problem of the car door
:

There are 3 closed doors, a car is parked behind one door, and goats are behind the rest. Only the host knows what is behind each door. Participants can choose a door. Before opening it, the host will open another door, exposing the goat behind the door, and then allow the participant to change their choice.

I would like to ask:

Seriously analyze, can you get a higher chance of getting a car if you don't change the option, or can you get a higher chance of getting a car if you change the option?

from random import * #Introduce random function library
x = 12345 #Number of games played
c = 0
#Number of initialization changes for selection uc = 0 #Number of initialization changes without selection
for i in range (1, x + 1):
a = randint (1,3) #The first selected door
b = randint (1,3) #The second selected door
if a == b:
uc + = 1 #When the selection is not changed, the number of times + 1
else :
c + = 1 #When changing the selection, increase the number of times by +1
print ("The probability of getting a car without changing the selection: {}". format (uc / x))
print (" The probability of getting a car by changing the selection: { } ". format (c / x))
---------------------------------------- --------------------- #The probability of getting a car without changing the
output
: 0.33584447144592955 The probability of getting
a car after changing the selection: 0.6641555285540705
 
4. Summary When
not changing the selection The probability of choosing a car is actually 1/3, either you choose the door of the car or you choose two sheep.
There are three situations when changing the selection. The first is that the car is selected at the beginning. After changing the selection, you get the sheep. Either the two sheep were chosen at the beginning, and the car must be selected when changing, so the probability is 2/3.
Explained in this way, plus the running of the program, we can know that the probability of getting a car will be greater when you change the choice!
————————————————
Copyright Statement: This article is an original article by CSDN blogger "Ultimate_dream", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprint .
Original link: https://blog.csdn.net/Ultimate_dream/article/details/105644648

Guess you like

Origin www.cnblogs.com/LQZ888/p/12753189.html