//Prompt the user to enter a number
printf("Please enter a number: ");
//Read number
scanf("%d", &number);
//Display number
printf("You entered %d", number);
printf("\n");
//Get a random number and scale to range 3..37
srand(time(NULL));
random_num = (37 - 3) + 1;
//Display random number
printf("Your random number is %d", rand() % random_num + 3);
// Set product to number * random number
product = multiply (number, random_num); //random_num equals 35 ?
如何存储为random_num生成的随机值以执行最后一次乘法?
此时用户输入的数字乘以35.这样每次都会产生一个随机数,最后该数字总是乘以35。
答案 0 :(得分:3)
这是因为你将random_num设置为(37-3)+ 1,即35。
你可以设置random_num = rand()%random_num + 3来存储rand()函数根据时间模38生成的随机数。
答案 1 :(得分:0)
您只需要创建一个新变量来存储它:
int my_random_number = rand() % random_num + 3;
//Display random number
printf("Your random number is %d", my_random_number);
//Do things with my_random_number