Python random library using a random method Detailed

This article describes the Python random random library to use Detailed, paper describes in great detail by the example code, a certain reference value of learning for all of us to learn or work, a friend in need can refer to the
Introduction

As we all know, python has extensive built-in library, also supports many third-party libraries, known as glue language, random library random, that comes with python standard library, his usage is too wide, in addition to generating relatively simple random number Outside , there are many features. Use random library:

import random

random library main function: Here Insert Picture Description
When the random library most of the functions you use, you need to design a sequence. If every time we do not want to define, but simply to remove some of the random numbers, letters combination, then you need to use another standard library string: import string
a string library is mainly used inside string constants defined: Here Insert Picture Description
Random use the library, for example:

1, the data file in random order (shuffle similar list, directly on the code, I believe could understand)

import random
#打开文件
f=open(r'F:\py\123.txt','r')
#读取文件信息,赋予一个变量
data=f.read()
#关闭文件
f.close()
print('数据:',data)
print('\n')
#data是一个字符串,我们以‘,'分隔成一个列表
s=data.split(',')
#随机排序
random.shuffle(s)
print(s)

Contents of the documents, you can also write a lot of lines, change what you can split a string when the above code. Here Insert Picture Description
Operating results (can run several times more than look at the results):

Data: A, B, C, D, E, F., G, H, the I, J, K
[ 'the I', 'K', 'G', 'J', 'D', 'E', 'F. ',' C ',' a ',' B ',' H ']
2, using a simple random random red released.

import random
def red_packet(total,num):
 for i in range(num-1):
  per=random.uniform(0.01,total/2)
  total=total- per
  print('%.2f'%per)
 else:
  print('%.2f'%total) 
red_packet(10,5)

Of course, to achieve the micro-letter envelopes like that is certainly much more complex, it involves algorithm, which only use the library to do a random example. operation result:

1.89
0.11
3.85
1.08
3.07

Similarly, more than a few times to try to run, it will be different.

3, string, and random combination of randomly generated verification code.

​import random
import string
s=string.digits + string.ascii_letters
v=random.sample(s,4)
print(v)
print(''.join(v))

operation result:

['2', 'T', 'd', 'H']
2TdH

We recommend the python learning sites , to see how old the program is to learn! From basic python script, reptiles, django, data mining

And other programming techniques, work experience, as well as senior careful study of small python partners to combat finishing zero-based information projects! everyday

Python programmers explain the timing of technology, sharing some learning methods and the need to pay attention to small details
than simply using the random library, there are many other uses, you can go to their own learning, and finally the introduction of a senior told me a the words: "Code this kind of thing, knock yourself again try to learn not only a cause, is lazy!!", we encourage each other.

Published 27 original articles · won praise 22 · views 20000 +

Guess you like

Origin blog.csdn.net/haoxun05/article/details/104382962