Last active
August 29, 2015 14:01
-
-
Save iBaozi/83157cdff3d3e28f32f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
__author__ = 'baozi' | |
import random | |
import time | |
start = time.clock() | |
prefix = '2' | |
length = 16 | |
num = 1000000 | |
time_str = time.strftime('%m%d_%H%I', time.localtime(time.time())) | |
destination_file = 'sn_%s_%d_%d_%s' % (prefix, length, num, time_str) | |
num_char_list = range(48, 58) # 0 - 9 | |
lower_letter_list = range(97, 123) # A - Z | |
upper_letter_list = range(65, 91) # a - z | |
char_list = num_char_list + upper_letter_list # use your char list | |
def gen_sn(): | |
remain_length = length | |
remain_length -= len(prefix) | |
sn = prefix | |
count = 0 | |
while count < remain_length: | |
sn += random.choice(char_list) | |
count += 1 | |
return sn | |
def int_to_char_in_list(int_list): | |
char_list = [] | |
for i in int_list: | |
char_list.append(chr(i)) | |
return char_list | |
def get_multi_sn(): | |
all_sn = {} | |
while len(all_sn) < num: | |
sn = gen_sn() | |
if not sn in all_sn: | |
all_sn[sn] = 0 | |
return all_sn | |
def main(): | |
sn_list = get_multi_sn() | |
sn_list_str = '' | |
for i in sn_list: | |
sn_list_str += i + '\n' | |
des_file = open(destination_file, 'w') | |
des_file.write(sn_list_str) | |
des_file.close() | |
use_time = time.clock() - start | |
print 'Use %.3f second(s)' % use_time | |
char_list = int_to_char_in_list(char_list) | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment