C语言中用random函数需要什么头文件

stephanie_sume 2010-05-18 03:35:41
在c语言中想用random(),需要什么头文件?

ps:加了"stdlib.h"
还是不对啊????
...全文
10205 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xmt_0114 2010-05-18
  • 打赏
  • 举报
回复
用rand()函数,不是random()
jianglutian 2010-05-18
  • 打赏
  • 举报
回复

Generates a pseudorandom number. A more secure version of this function is available, see rand_s.


int rand( void );


Return Value
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 (32767). Use the srand function to seed the pseudorandom-number generator before calling rand.

Requirements
Routine
Required header

rand
<stdlib.h>


For additional compatibility information, see Compatibility in the Introduction.

Example
Copy Code
// crt_rand.c
// This program seeds the random-number generator
// with the time, then exercises the rand function.
//

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

void SimpleRandDemo( int n )
{
// Print n random numbers.
int i;
for( i = 0; i < n; i++ )
printf( " %6d\n", rand() );
}

void RangedRandDemo( int range_min, int range_max, int n )
{
// Generate random numbers in the half-closed interval
// [range_min, range_max). In other words,
// range_min <= random number < range_max
int i;
for ( i = 0; i < n; i++ )
{
int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
+ range_min;
printf( " %6d\n", u);
}
}

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

SimpleRandDemo( 10 );
printf("\n");
RangedRandDemo( -100, 100, 10 );
}

Copy Code
22036
18330
11651
27464
18093
3284
11785
14686
11447
11285

74
48
27
65
96
64
-5
-42
-55
66

ikano 2010-05-18
  • 打赏
  • 举报
回复
#include <stdlib.h>
int rand(void);

例 n = rand( );范围是(0到32767)
老吴笔记 2010-05-18
  • 打赏
  • 举报
回复
stdlib.h
mian123yang 2010-05-18
  • 打赏
  • 举报
回复
顶下!
mstlq 2010-05-18
  • 打赏
  • 举报
回复
#include <stdlib.h>
void srand( unsigned seed );
int rand( void );


例子

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

srand( time(NULL) );
for( i = 0; i < 10; i++ )
printf( "Random number #%d: %d\n", i, rand() );

70,023

社区成员

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

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