python 利用random的shuffle洗牌方法生产1个12位数的随机密码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 利用random的shuffle洗牌方法生产1个12位数的随机密码相关的知识,希望对你有一定的参考价值。

  1 # !/usr/bin/env python                                                           
  2 # -*- coding:utf-8 -*-                                                           
  3 import random                                                                    
  4                                                                                  
  5 schr = [chr(s) for s in range(65,91)]                                            
  6 bchr = [chr(b) for b in range(97,123)]                                           
  7 num = [str(n) for n in range(0,10)]                                              
  8 signs = [‘!‘,‘@‘,‘#‘,‘$‘,‘%‘,‘^‘,‘&‘,‘*‘,‘(‘,‘)‘,‘+‘,‘-‘,‘_‘,‘=‘,‘<‘,‘>‘,‘`‘,‘|‘,‘[‘,‘]‘,‘{‘,‘}‘,‘?‘,‘/‘]
  9 codes = schr + bchr + num + signs                                                
 10                                                                                  
 11 def shuffle(codes):                                                              
 12     for i in range(5):                                                           
 13         random.shuffle(codes)                                                    
 14     return codes                                                                 
 15                                                                                  
 16 def get_code(codes):                                                             
 17     codes = shuffle(codes)                                                       
 18     rand_start = random.randint(0,len(codes)-12)                                 
 19     rand_end = rand_start + 12                                                   
 20     return ‘‘.join(codes[rand_start:rand_end])                                   
 21                                                                                  
 22 print get_code(codes)


本文出自 “bobo的运维之路” 博客,转载请与作者联系!

以上是关于python 利用random的shuffle洗牌方法生产1个12位数的随机密码的主要内容,如果未能解决你的问题,请参考以下文章

C++ STL应用与实现64: 如何使用shuffle和random_shuffle : 洗牌 (since C++11)

使用Fisher-Yates 洗牌算法实现random_shuffle函数

Python中的键控随机洗牌

Javascript 洗牌算法,打乱数组,随机获取元素

Fisher–Yates-shuffle 洗牌算法

洗牌算法浅试