Re: [shlug]用C语言产生随机数

24 views
Skip to first unread message

C.Chen

unread,
May 18, 2012, 10:02:13 AM5/18/12
to sh...@googlegroups.com
把srand放循环外面吧......

在 2012年5月18日 下午9:47,Giant Y <y560...@gmail.com> 写道:
> 我需要一个产生随机数的函数,随机数的范围是[0,1]。于是我写了如下代码:
>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <time.h>
>> double random();
>> int main()
>> {
>> int i;
>> for( i = 0; i < 10; ++i )
>> {
>> printf( "value:%.4f \n", random() );
>> }
>> return 0;
>> }
>> double random()
>> {
>> double value;
>> srand( (unsigned)time(NULL) );
>> value = rand()/32767.0;
>> return value;
>> }
>
>
> 结果很让我抓狂!请看:
>
>> D:\Public\temp\c_test>test
>> value:0.7725
>> value:0.7725
>> value:0.7725
>> value:0.7725
>> value:0.7725
>> value:0.7725
>> value:0.7725
>> value:0.7725
>> value:0.7725
>> value:0.7725
>> D:\Public\temp\c_test>test
>> value:0.7729
>> value:0.7729
>> value:0.7729
>> value:0.7729
>> value:0.7729
>> value:0.7729
>> value:0.7729
>> value:0.7729
>> value:0.7729
>> value:0.7729
>> D:\Public\temp\c_test>test
>> value:0.7740
>> value:0.7740
>> value:0.7740
>> value:0.7740
>> value:0.7740
>> value:0.7740
>> value:0.7740
>> value:0.7740
>> value:0.7740
>> value:0.7740
>
>
> 很明显,函数time(NULL)的返回值在连续产生随机数的过程中,显然来不及改变。请教应该如何做,才能连续产生看上去像真的随机的数字?
>
> PS:
> 我目前在研究优化算法,需要产生一个随机矩阵才能进行后续的计算。师兄在MATLAB中已经实现了算法的程序代码。虽然在MATLAB中产生一个随机矩阵很简单,但我想用C语言实现相同的效果。
> 一下是在MATLAB中产生的一个随机矩阵:
>
>>> x = rand(5,10)
>
> x =
>
> 0.4229 0.6999 0.5309 0.9686 0.7788 0.2810 0.5181
> 0.6761 0.2548 0.7805
> 0.0942 0.6385 0.6544 0.5313 0.4235 0.4401 0.9436
> 0.2891 0.2240 0.6753
> 0.5985 0.0336 0.4076 0.3251 0.0908 0.5271 0.6377
> 0.6718 0.6678 0.0067
> 0.4709 0.0688 0.8200 0.1056 0.2665 0.4574 0.9577
> 0.6951 0.8444 0.6022
> 0.6959 0.3196 0.7184 0.6110 0.1537 0.8754 0.2407
> 0.0680 0.3445 0.3868
>
>
>
> -> posted by Giant Y
>

罗健忠

unread,
May 18, 2012, 10:02:49 AM5/18/12
to sh...@googlegroups.com
要产生随机数的时候,srand调用一次就可以了,不然你10次循环,几乎都是相同的时间,肯定使用的是同一个随机种子,那算出来的结果肯定是一样的
把srand那句移到main中即可

2012/5/18 Giant Y <y560...@gmail.com>

C.Chen

unread,
May 18, 2012, 10:04:47 AM5/18/12
to sh...@googlegroups.com
或者调操作系统提供的函数

ning zhou

unread,
May 18, 2012, 10:09:57 AM5/18/12
to sh...@googlegroups.com
Sorry for the pingyin input method crashed in my ubuntu, sad~
So, please forgive my English. And my comments might be out of this topic much.

I suggest you don't use rand() to generate the random sequence, for few days ago one of my co-worker told me that is not accurate.

You'd better to learn that how to generate real random sequence in C code, there is a lot of algorithm you can find.

But no matter what method you are going to use, you should test 2 different sequences generated by your random(), to see if the correlation coefficient of these 2 sequences is 0 or not.


在 2012年5月18日 下午9:47,Giant Y <y560...@gmail.com>写道:



--
Best Regards.
Zhou Ning 周宁

Chaos Eternal

unread,
May 18, 2012, 10:14:52 AM5/18/12
to sh...@googlegroups.com

换成Linux然后读/dev/random

以后windows下的问题最好别拿来浪费大家的时间。

Giant Y

unread,
May 18, 2012, 9:47:38 AM5/18/12
to shlug

Lu Yiguang

unread,
May 18, 2012, 10:30:19 AM5/18/12
to sh...@googlegroups.com
在循环里面srand您碉堡了。。

依云

unread,
May 18, 2012, 1:54:07 PM5/18/12
to sh...@googlegroups.com
同意……不过看他一次就生成十个,我估计 /dev/random 不够他用的。/dev/urandom 应该也行吧。
> > -> posted by *Giant Y*
> >
> >

--
Best regards,
lilydjwg

Linux Vim Python 我的博客:
http://lilydjwg.is-programmer.com/
--
A: Because it obfuscates the reading.
Q: Why is top posting so bad?

Xunzhen Quan

unread,
May 18, 2012, 8:52:43 PM5/18/12
to sh...@googlegroups.com
真随机数仅靠电脑算法自身是无法产生的,Linux 的 /dev/random 会提供真随机数,这个真随机数是依靠捕获外部中断源的信息产生的,换句话说这些真随机数本质上是人给电脑提供的而不是电脑自己能产生的。

Giant Y

unread,
May 18, 2012, 9:30:47 PM5/18/12
to sh...@googlegroups.com
生成十个仅仅是测试。实际运算中需要一次产生300个随机数。

-> posted by  Giant Y



2012/5/19 依云 <lily...@gmail.com>

Giant Y

unread,
May 18, 2012, 9:43:05 PM5/18/12
to sh...@googlegroups.com
首先非常感谢您的解答。修改后的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

double random();

int main()
{
  int i;
  srand( (unsigned)time(NULL) );
  for( i = 0; i < 10; ++i )
    {
      printf( "value:%.4f \n", random() );
    }
  return 0;
}

double random()
{
  double value;
  //  srand( (unsigned)time(NULL) );
  value = rand()/32767.0;
  return value;
}

但是很遗憾,还是一次生成十个相同的“随机数”。

-> posted by  Giant Y



2012/5/18 罗健忠 <newke...@gmail.com>

Giant Y

unread,
May 18, 2012, 9:53:26 PM5/18/12
to sh...@googlegroups.com
很抱歉,这次真的一次产生十个不同的随机数。前面那次是我犯了个错误。

-> posted by  Giant Y



2012/5/19 Giant Y <y560...@gmail.com>
但是很遗憾,还是一次生成十个相同的“随机数”。

tom mario

unread,
May 19, 2012, 12:12:38 AM5/19/12
to sh...@googlegroups.com
<<c语言的科学与艺术>>里有讲这个,建议看一看,写的非常好

kaifeng jin

unread,
May 19, 2012, 1:58:37 AM5/19/12
to sh...@googlegroups.com
你把你的函数名改下吧,为什么你编译能通过,我用gcc编译结果是random()命名冲突了。。。改了个名后生成的是不同的数
king@king-MS-7750:~$ make 1
cc 1.c -o 1
1.c:5:8: error: conflicting types for ‘random’
/usr/include/stdlib.h:327:17: note: previous declaration of ‘random’ was here
1.c:18:8: error: conflicting types for ‘random’
/usr/include/stdlib.h:327:17: note: previous declaration of ‘random’ was here
make: *** [1] 错误 1
king@king-MS-7750:~$ make 1
cc 1.c -o 1
king@king-MS-7750:~$ ./1
value:62792.9802
value:26268.6625
value:16378.8950
value:58968.9066
value:18361.5140
value:37022.5151
value:25504.0539
value:16463.7409
value:59731.2690
value:14183.0327
这是我的编译信息,后来我改了个名

--
twitter:@zybest
新浪微博:@爱子悦
在openshift上搭建wordpress:http://blog-mking.rhcloud.com/

Yiling Cao

unread,
May 20, 2012, 11:06:01 PM5/20/12
to sh...@googlegroups.com
明显不懂srand的意思,上课没听

2012/5/19 kaifeng jin <jkf...@gmail.com>

Ma Xiaojun

unread,
May 21, 2012, 8:26:23 AM5/21/12
to sh...@googlegroups.com
2012/5/21 Yiling Cao <yilin...@gmail.com>:
> 明显不懂srand的意思,上课没听
这种随手查到的东西还用上课听?
http://manpages.ubuntu.com/manpages/precise/man3/srand.3.html

Lu Yiguang

unread,
May 21, 2012, 9:31:16 AM5/21/12
to sh...@googlegroups.com
这函数在pascal里面叫randomseed,然后从pascal转C的时候当时就懂了。。

强子

unread,
May 25, 2012, 2:27:26 AM5/25/12
to sh...@googlegroups.com
每次循环时候你的种子+1
Reply all
Reply to author
Forward
0 new messages