⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_58166.htm

📁 vc
💻 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 &lt;stdlib.h&gt;<BR>#include &lt;stdio.h&gt;<BR>#include &lt;time.h&gt;<BR><BR>void main( void )<BR>{<BR>&nbsp;&nbsp; int i;<BR><BR>&nbsp;&nbsp; /* Seed the random-number generator with current time so that<BR>&nbsp;&nbsp;&nbsp;&nbsp;* the numbers will be different every time we run.<BR>&nbsp;&nbsp;&nbsp;&nbsp;*/<BR>&nbsp;&nbsp; srand( (unsigned)time( NULL ) );<BR><BR>&nbsp;&nbsp; /* Display 10 numbers. */<BR>&nbsp;&nbsp; for( i = 0;&nbsp;&nbsp; i &lt; 10;i++ )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( &#34;&nbsp;&nbsp;%6d\n&#34;, 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 &lt;stdlib.h&gt; 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 &lt;stdlib.h&gt;<BR>#include &lt;stdio.h&gt;<BR>#include &lt;time.h&gt;<BR><BR>void main( void )<BR>{<BR>&nbsp;&nbsp; int i;<BR><BR>&nbsp;&nbsp; /* Seed the random-number generator with current time so that<BR>&nbsp;&nbsp;&nbsp;&nbsp;* the numbers will be different every time we run.<BR>&nbsp;&nbsp;&nbsp;&nbsp;*/<BR>&nbsp;&nbsp; srand( (unsigned)time( NULL ) );<BR><BR>&nbsp;&nbsp; /* Display 10 numbers. */<BR>&nbsp;&nbsp; for( i = 0;&nbsp;&nbsp; i &lt; 10;i++ )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( &#34;&nbsp;&nbsp;%6d\n&#34;, rand() );<BR>}<BR><BR><BR>Output<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;6929<BR>&nbsp;&nbsp;&nbsp;&nbsp;8026<BR>&nbsp;&nbsp; 21987<BR>&nbsp;&nbsp; 30734<BR>&nbsp;&nbsp; 20587<BR>&nbsp;&nbsp;&nbsp;&nbsp;6699<BR>&nbsp;&nbsp; 22034<BR>&nbsp;&nbsp; 25051<BR>&nbsp;&nbsp;&nbsp;&nbsp;7988<BR>&nbsp;&nbsp; 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&lt;100000;i++)<BR>{b=rand();<BR> ...............<BR> ..............<BR>}<BR>在这么短的执行过程中,我的系统只给我生成了4,5个随机数,不过我在MSDN的资料中看到过Random类,那会有帮助吗,但我的MSDN没有它的使用例子,谁能帮帮我啊?<BR>&nbsp;&nbsp; 
<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 + -