新型石蛙养殖场图片:我想用C实现随机取值。取5到18之间,请问怎办

来源:百度文库 编辑:高校问答 时间:2024/04/30 15:25:37
我想用C实现随机取值。取5到18之间,请问怎办

/* 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()%14+5 );
}
主要是这句:
rand()%14+5
rand()%14可以得到0至13的随机数,再加上5就行了。