24小时热门版块排行榜    

查看: 376  |  回复: 3
当前主题已经存档。

luckid913

新虫 (初入文坛)

[交流] matlab 产生均匀随机数

要用matlab产生一组0到1之间的均匀随机数。要求随机数是以计算机当前的时间作为种子的。如何实现?请各位指点,谢谢!
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

safiml

金虫 (小有名气)

★ ★ ★ ★ ★ ★
luckid913(金币+6,VIP+0):3Q for your help!! 7-10 18:54
转自http://hi.baidu.com/yilinghl/blo ... 8c4bfa3c7a18326acb8


从这儿看到的,rand()这个函数就是产生0~1的均匀分布的随机数,关键是以机器时间为种子,据这里说是rand()中引入clock参数即可,你看看吧

我贴一下吧:

rand产生的是0到1(不包括1)的随机数.

matlab的rand函数生的是伪随机数,即由种子递推出来的,相同的种子,生成相同的随机数.

matlab刚运行起来时,种子都为初始值,因此每次第一次执行rand得到的随机数都是相同的.

1.多次运行,生成相同的随机数方法:


用rand('state',S)设定种子
S为35阶向量,最简单的设为0就好

例:

rand('state',0);rand(10)

2. 任何生成相同的随机数方法:

试着产生和时间相关的随机数,种子与当前时间有关.

rand('state',sum(100*clock))

即:

rand('state',sum(100*clock)) ;rand(10)

只要执行rand('state',sum(100*clock)) ;的当前计算机时间不现,生成的随机值就不现.

也就是如果时间相同,生成的随机数还是会相同.

在你计算机速度足够快的情况下,试运行一下:

rand('state',sum(100*clock));A=rand(5,5);rand('state',sum(100*clock));B=rand(5,5);

A和B是相同.

所以建议再增加一个随机变量,变成:

rand('state',sum(100*clock)*rand(1));



%

据说matlab 的rand 函数还存在其它的根本性的问题,似乎是非随机性问题.

没具体研究及讨论,验证过,不感多言.

有兴趣的可以查阅:

<>


Petr Savicky
Institute of Computer Science
Academy of Sciences of CR
Czech Republic
savicky@cs.cas.cz
September 16, 2006
Abstract
The default random number generator in Matlab versions between 5 and at least
7.3 (R2006b) has a strong dependence between the numbers zi+1, zi+16, zi+28 in the
generated sequence. In particular, there is no index i such that the inequalities
zi+1 < 1/4, 1/4 zi+16 < 1/2, and 1/2 zi+28 are satisfied simultaneously. This
fact is proved as a consequence of the recurrence relation defining the generator. A
random sequence satisfies the inequalities with probability 1/32. Another example
demonstrating the dependence is a simple function f with values −1 and 1, such that
the correlation between f(zi+1, zi+16) and sign(zi+28 − 1/2) is at least 0.416, while it
should be zero.
A simple distribution on three variables that closely approximates the joint
distribution of zi+1, zi+16, zi+28 is described. The region of zero density in the
approximating distribution has volume 4/21 in the three dimensional unit cube. For
every integer 1 k 10, there is a parallelepiped with edges 1/2k+1, 1/2k and 1/2k+1,
where the density of the distribution is 2k. Numerical simulation confirms that the
distribution of the original generator matches the approximation within small random
error corresponding to the sample size.

[ Last edited by safiml on 2009-7-10 at 16:41 ]
2楼2009-07-10 16:37:06
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
3楼2009-07-10 16:38:24
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wxq_

金虫 (著名写手)

额的金币啊

顶楼上的
4楼2009-07-10 17:12:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 luckid913 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[博后之家] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +3 ero8OE6tv9cu 2026-09-02 6/300 2026-09-02 23:59 by rM1TE0WVDIIY
[公派出国] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 ero8OE6tv9cu 2026-09-02 3/150 2026-09-02 22:50 by rM1TE0WVDIIY
[基金申请] 为什么资助数各大高校都创新高,自己申请怎么就这么难 +14 Kittylucky 2026-08-27 15/750 2026-09-02 19:35 by liumei0601
[基金申请] 要骂人了,新模版改版就是要淡化问题凝练这种虚的东西,结果有个评委还在说凝练得不够 +9 瞬息宇宙 2026-08-31 17/850 2026-09-02 14:04 by anjeeshine
[基金申请] 基金系统什么内容也没有 30+4 winsaint 2026-08-27 10/500 2026-09-02 11:35 by 大不刘6
[基金申请] 学科评审组评审是指会评吗? +5 瞬息宇宙 2026-08-31 5/250 2026-09-02 10:13 by 雨冰共舞
[基金申请] 面上合作单位盖章 +6 ssyjh 2026-08-27 9/450 2026-09-01 20:01 by huagongfeihu
[论文投稿] 小白求助 投论文要求的highlights应该如何写 5+3 l1963982152 2026-08-29 4/200 2026-09-01 09:04 by 北京莱茵编辑
[基金申请] 麻烦专家们看看评委们的意见(F口面上) +7 gdd2018 2026-08-28 12/600 2026-09-01 08:32 by 尼古拉斯小虫
[基金申请] 国社科又开始会评了,不知道这次命运如何 +7 雨打竹帘 2026-08-30 11/550 2026-08-31 23:16 by hittle2008
[基金申请] 基金未中,这种答复是模板吗? +6 zhaosm1982 2026-08-27 7/350 2026-08-31 21:18 by qdxxmc
[基金申请] 面上意见出来了 +12 黄鸟于飞Chao 2026-08-29 23/1150 2026-08-31 18:57 by 黄鸟于飞Chao
[基金申请] 能否申诉? +7 echo8914667 2026-08-30 8/400 2026-08-31 17:00 by yihongxu
[基金申请] 中青基了要发朋友圈吗? +7 349506619 2026-08-28 7/350 2026-08-31 13:39 by 冼亮淀粉酶
[基金申请] 为什么到现在没收到通知? +5 tannykie 2026-08-29 5/250 2026-08-30 21:05 by purplejack
[基金申请] 有没有仍没收到信息的 +7 德尚中行 2026-08-27 8/400 2026-08-30 20:52 by purplejack
[考博] 找导师 +6 yuanjiabao 2026-08-29 7/350 2026-08-30 14:40 by 生科新手
[基金申请] 我就是申请一个面上项目而已,这评审意见是按照杰青的条件评的吧? +6 gouxfjh 2026-08-28 11/550 2026-08-30 07:57 by gouxfjh
[基金申请] 国自然评审意见 +13 wangmingqi 2026-08-28 19/950 2026-08-29 10:22 by Poppy1104
[基金申请] 2026年叶企孙基金 +4 bud_bud 2026-08-27 7/350 2026-08-29 07:23 by foolishmani
信息提示
请填处理意见