C 编程 - 当我点击输出时没有生成随机数
C Programming - Random Numbers aren't being generated when I hit output
//我的任务是创建一个掷骰子程序,生成1-6之间的随机数。
//我必须使用随机数来显示骰子在 60 次中落在数字上的次数。
//在此之后,我将不得不使用一个循环来生成 10*60 个随机数及其输出。
//我的问题是,我循环投掷的 10 个骰子中的每一个的输出都具有完全相同的输出。
//提前致谢:D
//Dice throwing program
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int N = 60, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0, table1();
int main()
{
int v, table;
for (v=0; v<10;v++)
{
table = table1();
}
return 0;
}
int table1()
{
int N = 60, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
printf("\nWhich numbers appeared when a dice is thrown 60 times:\n ");
srand(time(NULL));
//
int randArray[N],i;
for(i=0;i<N;i++)
randArray[i]= ((rand() %6)+1); //Generate number between 1 to 6
for(i=0;i<N;i++)
{
if (randArray[i] == 1)
count1++;
if (randArray[i] == 2)
count2++;
if (randArray[i] == 3)
count3++;
if (randArray[i] == 4)
count4++;
if (randArray[i] == 5)
count5++;
if (randArray[i] == 6)
count6++;
}
printf("\nThe number 1 has appeared %d times", count1);
printf("\nThe number 2 has appeared %d times", count2);
printf("\nThe number 3 has appeared %d times", count3);
printf("\nThe number 4 has appeared %d times", count4);
printf("\nThe number 5 has appeared %d times", count5);
printf("\nThe number 6 has appeared %d times\n", count6);
printf("\nTable displaying the number of times each number was rolled...\n");
printf("\n#1\t#2\t#3\t#4\t#5\t#6");
printf("\n%d\t%d\t%d\t%d\t%d\t%d", count1, count2, count3, count4, count5, count6);
return 0;
}
您的主要功能应如下所示:
int main()
{
int v, table;
srand(time(NULL));
for (v=0; v<10;v++)
{
table = table1();
}
return 0;
}
并从循环中删除 srand(time(NULL)) 行,因为您只需要初始化一次随机数生成器,正如@rossum 所说。
//我的任务是创建一个掷骰子程序,生成1-6之间的随机数。 //我必须使用随机数来显示骰子在 60 次中落在数字上的次数。 //在此之后,我将不得不使用一个循环来生成 10*60 个随机数及其输出。 //我的问题是,我循环投掷的 10 个骰子中的每一个的输出都具有完全相同的输出。 //提前致谢:D
//Dice throwing program
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int N = 60, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0, table1();
int main()
{
int v, table;
for (v=0; v<10;v++)
{
table = table1();
}
return 0;
}
int table1()
{
int N = 60, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
printf("\nWhich numbers appeared when a dice is thrown 60 times:\n ");
srand(time(NULL));
//
int randArray[N],i;
for(i=0;i<N;i++)
randArray[i]= ((rand() %6)+1); //Generate number between 1 to 6
for(i=0;i<N;i++)
{
if (randArray[i] == 1)
count1++;
if (randArray[i] == 2)
count2++;
if (randArray[i] == 3)
count3++;
if (randArray[i] == 4)
count4++;
if (randArray[i] == 5)
count5++;
if (randArray[i] == 6)
count6++;
}
printf("\nThe number 1 has appeared %d times", count1);
printf("\nThe number 2 has appeared %d times", count2);
printf("\nThe number 3 has appeared %d times", count3);
printf("\nThe number 4 has appeared %d times", count4);
printf("\nThe number 5 has appeared %d times", count5);
printf("\nThe number 6 has appeared %d times\n", count6);
printf("\nTable displaying the number of times each number was rolled...\n");
printf("\n#1\t#2\t#3\t#4\t#5\t#6");
printf("\n%d\t%d\t%d\t%d\t%d\t%d", count1, count2, count3, count4, count5, count6);
return 0;
}
您的主要功能应如下所示:
int main()
{
int v, table;
srand(time(NULL));
for (v=0; v<10;v++)
{
table = table1();
}
return 0;
}
并从循环中删除 srand(time(NULL)) 行,因为您只需要初始化一次随机数生成器,正如@rossum 所说。