Python生成随机字符串

利用Python生成随机域名等随机字符串。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from random import randrange, choice
from string import ascii_lowercase as lc
from sys import maxsize
from time import ctime tlds = ('com', 'edu', 'net', 'org', 'gov') for i in range(randrange(5, 110)):
dtint = randrange(maxsize/1000) # pick date
dtstr = ctime(dtint) # date string
llen = randrange(4, 8) # login is shorter
login = ''.join(choice(lc) for j in range(llen))
dlen = randrange(llen, 13)
dom = ''.join(choice(lc) for j in range(dlen))
print('%s::%s@%s.%s::%d-%d-%d' % (dtstr, login, dom, choice(tlds), dtint, llen, dlen))

该脚本生成拥有三个字段的字符串,由一对冒号或者一对双冒号分隔。第一个字段是随机整数,该整数将被转换为一个日期。下一个字段是一个随机生成的电子邮件地址。最后一个字段是一个由单横线(-)分隔的整数集。

输出结果如下

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/baoshan/Documents/03.python/Python入门经典/python/test.py
Sat Feb 6 23:39:33 18080579::ahfvd@qgoefgyxbf.com::570505799633973-5-10
Fri Aug 28 21:00:12 149488065::ypliyfg@brmgaaig.net::4717325545362012-7-8
Tue Aug 29 19:13:28 189633561::xwoxq@ixrfi.net::5984195035662808-5-5
Sun Mar 9 16:01:45 127723924::svev@ytiqxqemalrp.org::4030515577555305-4-12
Sun Feb 24 22:02:17 15157754::txkn@ykwbikcbeapj.com::478270352959337-4-12
Sat Apr 1 02:16:35 71501341::bksggqn@jsykydqtzkjd.gov::2256302226478595-7-12
Sun Jul 6 05:02:28 18939664::yoxlbzt@nsxwkkispsyb.com::597615916712548-7-12
Wed Jun 29 17:17:14 114444146::saqfaj@zahfeokrohmn.gov::3611446270334234-6-12
Fri May 19 15:08:14 234054828::tydg@tvbgjmaxhpjh.gov::7385994817398494-4-12
Fri Nov 16 04:22:12 269978536::kxidgub@qrxydwffkn.edu::8519637561999732-7-10
Fri Jul 8 08:51:02 132470935::fdqpt@ktpgxag.org::4180316786239862-5-7
Wed Aug 10 16:48:49 36911560::egulsg@goodvga.edu::1164754179132529-6-7
Sat Nov 29 23:20:26 149477394::xghxqb@ciornmdydk.org::4716988809146426-6-10
Fri Jul 7 13:19:11 152052986::iesf@rgfusbbbvx.edu::4798266629692751-4-10
Fri Nov 20 18:10:08 184687953::znpxukm@bmrwtlog.edu::5828126728558208-7-8
Thu May 14 20:58:39 74067665::fydupp@htpygpdjg.org::2337287593582719-6-9
Wed Dec 4 16:20:16 6375405::stdrqp@ematyjde.gov::201126211518016-6-8
Tue Oct 2 21:39:46 105796046::yfwpgp@jaajtaeb.com::3338538601988386-6-8
Wed Oct 29 01:21:40 242249941::elio@pxrkwppeutnq.edu::7644607618929700-4-12
Fri Oct 28 14:23:42 225762907::kube@xbrldbnh.edu::7124327078307822-4-8
Fri May 17 05:26:04 237385793::dryteu@thtqbwjb.gov::7491109919808364-6-8
Sat Nov 9 06:58:43 32473963::zgiblz@srudpmx.edu::1024717151372323-6-7
Tue Jan 29 11:24:31 48403935::tpwan@ixqgar.edu::1527418488597871-5-6
Sat Jul 16 01:41:29 63586450::pxusn@oiatmeawusm.net::2006532400268489-5-11
Tue Jun 17 15:53:16 125570492::ygspkq@dqruehtdve.net::3962559836044396-6-10
Fri Jul 6 18:02:18 110124485::lltpzu@axyyicbojkt.org::3475130936176938-6-11
Mon Nov 6 02:59:19 128389967::jrevq@brgkadkgufd.org::4051533885361159-5-11
Wed Aug 29 07:26:21 231020587::wzxk@nmmbzxx.edu::7290243428541981-4-7
Wed Feb 12 04:46:47 44084358::zgek@puujor.gov::1391105805770807-4-6
Thu Mar 6 21:35:05 269774380::snagtj@dezvtkphuzo.edu::8513194998922505-6-11
Sun Aug 1 04:50:36 5675115::lcidrip@djiieqhgrn.com::179027182702236-7-10
Fri Jan 30 11:55:08 173445807::rlyux@xhwgfddvlco.edu::5473358841383708-5-11
Mon Nov 23 15:06:56 92711344::fvcud@krqmogw.net::2925625293529616-5-7
Fri Sep 1 18:57:03 122804541::zqehfs@rcyddelnh.gov::3875274859604223-6-9
Thu Aug 19 06:30:59 225684934::jhozy@ijjiupyjvcw.gov::7121866482052259-5-11
Tue Jan 13 23:40:47 220538415::tbqhg@vadjxsvxuw.com::6959458010216447-5-10
Mon Jan 29 19:35:42 156070638::lboijsg@vahdhbm.org::4925051467241742-7-7
Tue Jun 9 14:24:17 10843671::fwanqzg@gytegglv.org::342131051859857-7-8
Mon Oct 21 00:17:29 28336307::edilf@ewfwidreylj.net::894145337885849-5-11
Wed Aug 7 11:14:04 290314458::yfvoxn@gavgkqnuljb.gov::9161377267720444-6-11
Sun Nov 4 20:56:12 249269523::lratvzz@yqsfhrqqbe.com::7866124231697772-7-10
Sun Oct 31 16:30:24 151044100::lyexl@bmege.net::4766429272638624-5-5
Tue Mar 22 17:36:01 205694935::jvijdr@firuvgoonzzd.org::6491043030188161-6-12
Sat Mar 1 10:14:01 99822200::pucbbnq@nhcyylptxyh.org::3150022211864041-7-11
Mon Jan 8 03:56:40 58937049::vxhrbea@wjigyufx.net::1859811459739000-7-8
Tue Jul 3 23:50:07 141736610::ekhy@oynkrh.com::4472713247068207-4-6
Wed Apr 5 02:00:40 211115786::jfwetyt@wrgmmvn.net::6662108566202440-7-7
Sat Mar 6 15:37:16 227311288::qhne@ojnknexf.net::7173189242955436-4-8
Wed Mar 10 00:24:24 220025756::pwlip@ryzwucuxhcwg.org::6943280059585464-5-12
Wed Jun 21 17:55:56 164719426::zlew@wjtjqvm.org::5197980867357356-4-7
Fri Sep 14 08:34:01 219967392::klaqp@hzgegttbqapj.gov::6941438285934841-5-12
Fri Feb 15 10:49:01 200550532::ssqag@skqddvk.edu::6328701348576541-5-7
Tue Nov 4 23:24:52 141649519::crjxp@rosolunkc.edu::4469964931236292-5-9
Thu May 26 12:30:05 82025836::lqddxnm@ohmzauofisgj.edu::2588423214803405-7-12
Wed Dec 23 18:00:32 127106522::ynkp@usruda.gov::4011032277252032-4-6
Mon Feb 1 03:20:18 25237909::uqiulc@vscgxwjpf.edu::796369318341618-6-9
Tue Nov 21 08:08:53 93402215::xnrey@tovrxfxks.net::2947427076211733-5-9
Thu Jul 27 19:23:06 134687724::txjdc@djaxu.edu::4250271892044186-5-5
Mon Mar 14 23:19:16 228798687::mjvd@dieplzicmcl.org::7220127022413556-4-11
Wed Mar 31 03:42:05 209315677::wvwg@cwxcebihymce.edu::6605302612506125-4-12
Wed Nov 8 00:01:06 236681578::puyepci@ycfgepc.org::7468887055881666-7-7 Process finished with exit code 0

上述参考自:Python核心编程

谢谢

上一篇:[Windows Phone] 如何在 Windows Phone 应用程式制作市集搜寻


下一篇:javascript创建自定义对象和prototype