易截截图软件、单文件、免安装、纯绿色、仅160KB

python random 随机的问题

在一个项目中需要获取随机数,谁知道遇到点问题:随机数不随机。所以我写了个简单原型。看下到底是啥问题。
import os,random,sys,time
while True:
father = os.fork()
if father:
time.sleep(2)
rd = 7
else:
#random.seed()
rd = random.choice([2,3,4,5])
print rd
time.sleep(2)
sys.exit(0)

代码基本就这样。谁知道在子进程里面打印的 random  不起作用。每次随机数都是一样的。
测试发现。是 while 出现问题,因为while 一直循环,而随机种子,是在第一次 import random 的时候就已经种下了。所以导致随机数一直都不会变化~~~  汗。幼稚问题导致浪费很多时间
看random.seed手册解释:
If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported.
明白了。最后用 random.seed() 来改变随机种子。。


相关文档:

Python在windows下的路径问题

    windows下的路径像“f:\program files\python\backup”其中“\”需要用转义符,写成“\\”,或者前面加r写成path
= r’f:\program files\python\backup’但是在调用一些系统命令,如os.system(rar a path e:\backup)会出错,原因是“program files&rdquo ......

【zz】Python常用资源网址

zz from 《可爱的Python》
http://www.woodpecker.org.cn/
Python标准库 http://www.woodpecker.org.cn:9081/doc/Python/_html/PythonStandardLib/
简明Python教程 http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/index.html
Python快速介绍 http://www.zoomquiet.org/share/s5/intropy/070322-intro ......

我的VIM配置(ubuntu+python)

1,不用修改/etc/vim下的vimrc及gvimrc文件 。。。
      2,在~目录下,新建一个.vimrc的配置文件。内容如下:(高亮,自动对齐,自动缩进,显示行号)
" An example for a vimrc file.
"
" Maintainer: Bram Moolenaar <Bram@vim.org>
" Last change: 2006 Nov ......

python语法和代码约定

1. 第二章 语法及代码约定                       
                      &nb ......

beginning python summary chapter 3 使用字符串

0、字符串是不可变的。
1、基本字符串操作:索引、切片、复制、成员、长度、最大和最小。
2、字符串格式化:用格式化操作符百分号%实现,eg:>>> format = "Hello, %s. %s enough for ya?"
                    ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号