c语言随机函数怎么用,怎么定义?
来源:百度知道 编辑:UC知道 时间:2024/05/30 22:51:56
我用的是dev-c++这个编译器,谢谢 !!
这个编译器在运行的时候,写什么才能输出呢, 是
头文件 include“conio.h”
{
getch();
} 是这个吗 ?/ 谢谢了。 使用随机函数,怎么定义。
这个编译器在运行的时候,写什么才能输出呢, 是
头文件 include“conio.h”
{
getch();
} 是这个吗 ?/ 谢谢了。 使用随机函数,怎么定义。
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i;
printf("Ten random numbers from 0 to 99\n");
for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
return 0;
}
srand初始化随机数种子
rand()随机数
随机数函数:int rand();
使用:
int value = rand();
value 将被随机的赋一个值,但是这样编译以后,每次的运行结果实际上是一样的,也就是同一个随机数。所以提供了随机数种子这个函数: srand(int)
使用:
srand(n);
int value = rand();
第二个问题:
输入输出大多定义在 stdio.h 里面,先写一个 hello world 吧。
头文件#include<stdlib.h>
C函数名 rand
功 能: 随机数发生器
用 法:
需要先调用srand初始化
void rand(void);
程序例:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main( void )
{
int i,k;
srand( (unsigned)time( NULL ) );
for( i = 0; i < 10;i++ )
{
k=ra