📄 subject_58166.htm
字号:
<p>
序号:58166 发表者:陈昕 发表日期:2003-10-30 14:33:20
<br>主题:各位大侠,请教一个很菜的问题
<br>内容:我不知道在c++下怎么产生随机数,我找了三本书,也看了msdn。但还是找不到,我对类完全不了解。(大二学生,才学过c)问老师,那些动口不动手的白痴也讲不清,所以来请教大家了,请告诉我一个算法,和必须include的文件。
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:ohye212 回复日期:2003-10-30 14:50:19
<br>内容:#include <stdlib.h><BR>#include <stdio.h><BR>#include <time.h><BR><BR>void main( void )<BR>{<BR> int i;<BR><BR> /* Seed the random-number generator with current time so that<BR> * the numbers will be different every time we run.<BR> */<BR> srand( (unsigned)time( NULL ) );<BR><BR> /* Display 10 numbers. */<BR> for( i = 0; i < 10;i++ )<BR> printf( " %6d\n", rand() );<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:michael 回复日期:2003-10-30 14:51:30
<br>内容:int rand( void );<BR><BR>Routine Required Header Compatibility <BR>rand <stdlib.h> ANSI, Win 95, Win NT <BR><BR><BR>For additional compatibility information, see Compatibility in the Introduction.<BR><BR>Libraries<BR><BR>LIBC.LIB Single thread static library, retail version <BR>LIBCMT.LIB Multithread static library, retail version <BR>MSVCRT.LIB Import library for MSVCRT.DLL, retail version <BR><BR><BR>Return Value<BR><BR>rand returns a pseudorandom number, as described above. There is no error return.<BR><BR>Remarks<BR><BR>The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand.<BR><BR>Example<BR><BR>/* RAND.C: This program seeds the random-number generator<BR> * with the time, then displays 10 random integers.<BR> */<BR><BR>#include <stdlib.h><BR>#include <stdio.h><BR>#include <time.h><BR><BR>void main( void )<BR>{<BR> int i;<BR><BR> /* Seed the random-number generator with current time so that<BR> * the numbers will be different every time we run.<BR> */<BR> srand( (unsigned)time( NULL ) );<BR><BR> /* Display 10 numbers. */<BR> for( i = 0; i < 10;i++ )<BR> printf( " %6d\n", rand() );<BR>}<BR><BR><BR>Output<BR><BR> 6929<BR> 8026<BR> 21987<BR> 30734<BR> 20587<BR> 6699<BR> 22034<BR> 25051<BR> 7988<BR> 10104<BR>摘自《MSDN》
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:陈昕 回复日期:2003-10-30 19:42:56
<br>内容:能具体告诉我生成0-52之间的随机数的算法,或是你的算法生成数的最大值吗?小弟谢谢了。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:汪永明 回复日期:2003-10-30 20:08:30
<br>内容:你把srand()和rand()生成的随机数拿来对53取余啊。就有0-52的数了。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:陈昕 回复日期:2003-10-31 08:52:02
<br>内容:谢谢各位的指导,不过小弟我的问题没完全解决。你们给的方法只能在一个很短的时间内生成一个随机数,但我要把随机数生成在一个这样的程序里:<BR>srand( (unsigned)time( NULL ) );<BR>for(i=1;i<100000;i++)<BR>{b=rand();<BR> ...............<BR> ..............<BR>}<BR>在这么短的执行过程中,我的系统只给我生成了4,5个随机数,不过我在MSDN的资料中看到过Random类,那会有帮助吗,但我的MSDN没有它的使用例子,谁能帮帮我啊?<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -