Python±ê×¼¿â randomÄ£¿é
Python±ê×¼¿â-randomÄ£¿é
random Ä£¿é°üº¬Ðí¶àËæ»úÊýÉú³ÉÆ÷. »ù±¾Ëæ»úÊýÉú³ÉÆ÷(»ùÓÚ Wichmann ºÍ Hill , 1982 µÄÊýѧÔËËãÀíÂÛ) ¿ÉÒÔͨ¹ýºÜ¶à·½·¨·ÃÎÊ, Èç Example 2-29 Ëùʾ. 2.17.0.1. Example 2-29. ʹÓà random Ä£¿é»ñµÃËæ»úÊý×Ö File: random-example-1.py import random for i i
¡¡¡¡
random Ä£¿é°üº¬Ðí¶àËæ»úÊýÉú³ÉÆ÷.
»ù±¾Ëæ»úÊýÉú³ÉÆ÷(»ùÓÚ Wichmann ºÍ Hill , 1982 µÄÊýѧÔËËãÀíÂÛ) ¿ÉÒÔͨ¹ýºÜ¶à·½·¨·ÃÎÊ, Èç Example 2-29 Ëùʾ.
2.17.0.1. Example 2-29. ʹÓà random Ä£¿é»ñµÃËæ»úÊý×Ö
File: random-example-1.py
import random
for i in range(5):
# random float: 0.0 <= number < 1.0
print random.random(),
# random float: 10 <= number < 20
print random.uniform(10, 20),
# random integer: 100 <= number <= 1000
print random.randint(100, 1000),
# random integer: even numbers in 100 <= number < 1000
print random.randrange(100, 1000, 2)
0.946842713956 19.5910069381 709 172
0.573613195398 16.2758417025 407 120
0.363241598013 16.8079747714 916 580
0.602115173978 18.386796935 531 774
0.526767588533 18.0783794596 223 344
×¢ÒâÕâÀïµÄ randint º¯Êý¿ÉÒÔ·µ»ØÉϽç, ¶øÆäËûº¯Êý×ÜÊÇ·µ»ØСÓÚÉϽçµÄÖµ. ËùÓк¯Êý¶¼ÓпÉÄÜ·µ»ØϽçÖµ.
Example 2-30 չʾÁË choice º¯Êý, ËüÓÃÀ´´ÓÒ»¸öÐòÁÐÀï·Ö¼ð³öÒ»¸öËæ»úÏîÄ¿. Ëü¿ÉÒÔÓÃÓÚÁбí, Ôª×é, ÒÔ¼°ÆäËûÐòÁÐ(µ±È», ·Ç¿ÕµÄ).
2.17.0.2. Example 2-30. ʹÓà random Ä£¿é´ÓÐòÁÐÈ¡³öËæ»úÏî
File: random-example-2.py
import random
# random choice from a list
for i in range(5):
print random.choice([1, 2, 3, 5, 9])
2
3
1
9
1
ÔÚ 2.0 ¼°ÒÔºó°æ±¾, shuffle º¯Êý¿ÉÒÔÓÃÓÚ´òÂÒÒ»¸öÁбíµÄÄÚÈÝ (Ò²¾ÍÊÇÉú³ÉÒ»¸ö¸ÃÁбíµÄËæ»úÈ«ÅÅÁÐ). Example 2-31 չʾÁËÈçºÎÔھɰ汾ÖÐʵÏָú¯Êý.
2.17.0.3. Example 2-31. ʹÓà random Ä£¿é´òÂÒÒ»¸±ÅÆ
File: random-example-4.py
import random
try:
# available in 2.0 and later
shuffle = random.shuffle
except AttributeError:
def shuffle(x):
for i in xrange(len(x)-1, 0, -1):
# pick an element in x[:i+1] with which to exchange x[i]
j = int(random.random() * (i
Ïà¹ØÎĵµ£º
Python ×Öµä
×ÖµäÀàËÆÓÚÄãͨ¹ýÁªÏµÈËÃû×Ö²éÕÒµØÖ·ºÍÁªÏµÈËÏêϸÇé¿öµÄµØÖ·²¾£¬¼´£¬ÎÒÃǰѼü£¨Ãû×Ö£©ºÍÖµ£¨ÏêϸÇé¿ö£©ÁªÏµÔÚÒ»Æð¡£×¢Ò⣬¼ü±ØÐëÊÇΨһµÄ£¬¾ÍÏñÈç¹ûÓÐÁ½¸öÈËÇ¡ÇÉͬÃûµÄ»°£¬ÄãÎÞ·¨ÕÒµ½ÕýÈ·µÄÐÅÏ¢¡£
×¢Ò⣬ÄãÖ»ÄÜʹÓò»¿É±äµÄ¶ÔÏ󣨱ÈÈç×Ö·û´®£©À´×÷Ϊ×ÖµäµÄ¼ü£¬µ«ÊÇÄã¿ÉÒÔ²»¿É±ä»ò¿É±äµÄ¶ÔÏó×÷Ϊ×Öµäµ ......
1 #!/usr/bin/python ʹÓÃħ·¨×Ö·ûµ÷ÓÃpython
2
3 from sys import argv µ¼ÈësysÊǵ¼Èëpython½âÊÍÆ÷ºÍËû»·¾³Ïà¹ØµÄ²ÎÊý
4 from os import makedirs,unlink,sep
osÖ÷ÒªÌṩ¶Ôϵͳ·¾¶£¬ÎļþÖØÃüÃûºÍɾ³ýÎļþËùÐèµÄº¯Êý
makedirsÊÇ´´½¨µÝ¹éÎļþ¼ÐµÄº¯Êý¡£±ÈÈç˵ÎÒÃÇÒª´´½¨Ò»¸öеÄĿ¼£¬/python/HTML/crawl,µ«ÊÇÄ ......
Áбí
listÊÇ´¦ÀíÒ»×éÓÐÐòÏîÄ¿µÄÊý¾Ý½á¹¹£¬¼´Äã¿ÉÒÔÔÚÒ»¸öÁбíÖд洢һ¸ö ÐòÁÐ
µÄÏîÄ¿¡£¼ÙÏëÄãÓÐÒ»¸ö¹ºÎïÁÐ±í£¬ÉÏÃæ¼ÇÔØ×ÅÄãÒªÂòµÄ¶«Î÷£¬Äã¾ÍÈÝÒ×Àí½âÁбíÁË¡£Ö»²»¹ýÔÚÄãµÄ¹ºÎï±íÉÏ£¬¿ÉÄÜÿÑù¶«Î÷¶¼¶À×ÔÕ¼ÓÐÒ»ÐУ¬¶øÔÚPythonÖУ¬
ÄãÔÚÿ¸öÏîÄ¿Ö®¼äÓöººÅ·Ö¸î¡£
ÁбíÖеÄÏîÄ¿Ó¦¸Ã°üÀ¨ÔÚ·½À¨ºÅÖУ¬ÕâÑùPython¾ÍÖªµÀÄãÊÇ ......
Ç°¼¸Ìì¿´µ½ÁËÒ»ÐÐÇó1000µÄ½×³ËµÄPython´úÂ룺
print
reduce
(
lambda
x
,
y
:
x
*
y
,
range
(
1
,
1001
))
Ò»ÏÂ×Ó±»python´úÂëµÄ¾«¼ò
Óë½ô´ÕËùÕÛ·þ£¬¹Ê¶Ô´úÂë½øÐÐÁ˼òµ¥µÄ·ÖÎö¡£
reduceÓërange¶¼ÊÇPythonµÄÄÚÖú¯Êý¡£
range£¨1£¬10 ......
Ïȸø³öÒ»¸öËÄÈËÍŶÔ
Decorator mode
µÄ¶¨Ò壺
¶¯Ì¬µØ
¸øÒ»¸ö
¶ÔÏó
Ìí¼ÓһЩ
¶îÍâµÄÖ°Ôð
¡£
ÔÙÀ´ËµËµÕâ¸öģʽµÄºÃ´¦£ºÈÏÖ¤£¬È¨ÏÞ¼ì²é£¬¼ÇÈÕÖ¾£¬¼ì²é²ÎÊý£¬¼ÓËø£¬µÈµÈµÈµÈ£¬ÕâЩ¹¦ÄܺÍϵͳҵÎñÎ޹أ¬µ«ÓÖÊÇϵͳËù±ØÐëµÄ£¬ËµµÄ¸üÃ÷°×Ò»µã£¬¾ÍÊÇÃæÏò·½ÃæµÄ±à³Ì£¨
AOP
£©¡£
AOP
°ÑÓëÒµÎñÎ޹صĴúÂëÊ®·Ö¸É¾» ......