C++编程产生1-10的随机数

来源:百度知道 编辑:UC知道 时间:2024/05/09 11:41:45
请问我想产生1-10的随机数是怎么产生的?在C++的编程环境下

直接用rand就可以了啊。
/*************************************/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main( void )
{
int i;
srand( (unsigned)time( NULL ) );
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main( void )
{
int i;
srand( (unsigned)time( NULL ) );

printf( " %6d\n", rand()%10+1 );
}

srand(设置随机数种子)
相关函数 rand,random srandom

表头文件 #include<stdlib.h>

定义函数 void srand (unsigned int seed);

函数说明 srand()用来设置rand()产生随机数时的随机数种子。参数seed必须是个整数,通常可以利用geypid()或time(0)的返回值来当做seed。如果每次seed都设相同值,rand()所产生的随机数值每次就会一样。

返回值

范例 /* 产生介于1 到10 间的随机数值,此范例与执行结果可与rand()参照*/
#include<time.h>
#include<stdlib.h>
main(