请问如何在vc中如何产生随机数?

cracklife 2003-05-18 09:50:36
我没有学过mfc,请问如何用rnd产生随机数,应该包含什么样的头文件?
...全文
98 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lijdking 2003-06-04
  • 打赏
  • 举报
回复
1/(rand()+1) //只能产生0而不能产生1?
arfi 2003-05-21
  • 打赏
  • 举报
回复
1/(rand()+1)
cracklife 2003-05-21
  • 打赏
  • 举报
回复
那产生0~1之间的随机数如何产生?
ghtsao 2003-05-19
  • 打赏
  • 举报
回复
我也来抢点分:

rand

Generates a pseudorandom number.

Routine Required Header
rand <stdlib.h>


int rand( void );
Libraries
All versions of the C run-time libraries.

Return Values
rand returns a pseudorandom number, as described above. There is no error return.

Remarks
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.

Example
/* RAND.C: This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/

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

void main( void )
{
int i;

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}

Output
6929
8026
21987
30734
20587
6699
22034
25051
7988
10104



zhaozhoujsj 2003-05-18
  • 打赏
  • 举报
回复
#include "windows.h"
#include "stdio.h"
#include "time.h"
void main()
{srand( (unsigned)time( NULL ) );

/*产生10个100-1000的随机数*/
for( int i = 0; i < 10;i++ )
printf( " %6d\n", rand()%901+100 );
}
tonewangl 2003-05-18
  • 打赏
  • 举报
回复
我在msdn里找了这个函数,希望对你又帮助
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( void )
{
int i;
srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}


tonewangl 2003-05-18
  • 打赏
  • 举报
回复
你可以用rand函数,random函书是c\c++函数,十分容易,查查msdn就行了

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧