c語言中怎么讓電腦隨機產(chǎn)生數(shù)
慕粉15279446966
2016-10-15 10:29:45
TA貢獻149條經(jīng)驗 獲得超291個贊
使用rand函數(shù),
如果不指定范圍按1樓操作就好,指定要x-y范圍的隨機數(shù)的話就rand()%(y-x+1)+x
比如要求1~10之間的隨機數(shù) 就是rand()%10+1
TA貢獻66條經(jīng)驗 獲得超26個贊
#include <stdio.h>
#include <stdlib.h>
#include <time.h> //用到了time函數(shù)
int main()
{ ?int i,ran;
? srand((unsigned) time(NULL)); //用時間做種,每次產(chǎn)生隨機數(shù)不一樣
? for (i=0; i<20; i++)
? {
? ? ran = rand() % 101; ?//產(chǎn)生0-100的隨機數(shù)
? ? printf("%d ", ran);
? } ?
? return 0;
}
舉報