//My Task is to create a dice throwing program that generates random numbers between 1-6. //I had to use the random numbers to show how many times the dice landed on a number in 60 goes. //After this, I would have to use a loop to generate 10*60 of the random numbers and their outputs. //My problem is that the Output for each of the 10 dice throws I looped have the same exact output. //Thanks In advance :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;
}
Aucun commentaire:
Enregistrer un commentaire