C/C++问题,请问如果我要在1,2,3,4这四个已知数中随机抽取一个数,需要用到哪些函数?

如题所述

第1个回答  2011-02-27
#include"stdio.h"
#include"stdlib.h"
int main(){
while(1){
printf("%d\n",rand()%3+1);
system("pause");
}
return 0;
}本回答被提问者采纳
第2个回答  2011-02-25
#include<ctime>
srand((unsigned)time(NULL));
int num;
num=rand()%4+1;
//rand()%n意思是随机生成从0到n的数值,srand((unsigned)time(NULL)),以时间作为生成种子追问

请问“rand()%4+1”这个可不可以当作一个变量来用?那个“rand()%4+1”是不是在程序中每出现一次,它的值就会不同?如果是,那么有没有可以控制它的值在某段时间内不变的方法?

追答

比如说Int a=rand()%+1;
a就是随机赋值的,

相似回答