Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
rand()未给出指定数字范围内的值(在C中)_C_Random - Fatal编程技术网

rand()未给出指定数字范围内的值(在C中)

rand()未给出指定数字范围内的值(在C中),c,random,C,Random,我只想让它给我一个介于1和6之间的值,但它给了我这个值: P1d1 = 1445768086 P1d2 = -2 P2d1 = 1982468450 P2d2 = 198281572 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <time.h> int main (){ srand(time(NULL)); /*Player 1*/ int P1d1 = 1 + rand()

我只想让它给我一个介于1和6之间的值,但它给了我这个值:

P1d1 = 1445768086

P1d2 = -2

P2d1 = 1982468450

P2d2 = 198281572
这是我的密码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (){

srand(time(NULL));

/*Player 1*/

int P1d1 = 1 + rand() % 6;  //Rolls Player 1's first die; random # between 1-6
int P1d2 = 1+ rand() % 6;  //Rolls Player 1's second die; random # between 1-6
int P1total = P1d1 + P1d2;  //Takes total of both rolls

/*Player 2*/

int P2d1 = 1 + rand() % 6; //Rolls Player 2's first die; random # between 1-6
int P2d2 = 1 + rand() % 6; //Rolls Player 2's second die; random # between 1-6
int P2total = P2d1 + P2d2; //Takes total of both rolls

printf("P1d1 = %d\nP1d2 = %d\nP2d1 = %d\n P2d2 = %d\n");

}
#包括
#包括
#包括
int main(){
srand(时间(空));
/*玩家1*/
int P1d1=1+rand()%6;//掷玩家1的第一个骰子;随机#在1-6之间
int P1d2=1+rand()%6;//掷玩家1的第二个骰子;随机#在1-6之间
int P1total=P1d1+P1d2;//取两卷的总和
/*玩家2*/
int P2d1=1+rand()%6;//掷玩家2的第一个骰子;随机#在1-6之间
int P2d2=1+rand()%6;//掷玩家2的第二个骰子;随机#在1-6之间
int P2total=P2d1+P2d2;//取两卷的总和
printf(“P1d1=%d\nP1d2=%d\nP2d1=%d\n P2d2=%d\n”);
}

我不允许使用函数,因为我们还没有在课堂上讨论它们。非常感谢您的帮助

您的
printf
没有指定变量。因此,您得到的是随机垃圾打印,而不是您要查找的实际变量值

你应该有这个:

printf("P1d1 = %d\nP1d2 = %d\nP2d1 = %d\n P2d2 = %d\n", P1d1, P1d2, P2d1, P2d2);

您的
printf
没有指定变量。因此,您得到的是随机垃圾打印,而不是您要查找的实际变量值

你应该有这个:

printf("P1d1 = %d\nP1d2 = %d\nP2d1 = %d\n P2d2 = %d\n", P1d1, P1d2, P2d1, P2d2);

这是您实际的
printf()
行吗?您忘记了变量。现在的代码是,不应该输出任何内容
printf
没有给任何变量print@Pankrates这是一种未定义的行为,所以可能会发生任何事情,不打印是一种可能的结果。@您想评论一下这个问题的错误吗?这是您实际的
printf()
行吗?您忘记了变量。现在的代码是,不应该输出任何内容
printf
没有给任何变量print@Pankrates这是一种未定义的行为,所以任何事情都有可能发生,不打印任何内容是一种可能的结果。@选民是否愿意评论这个问题的错误?