Hi. I wrote a code that rolls a dice 3000 times and counts number of sixes rolled then prints the amount. I noticed that no matter how many times I ran the code, I didn't get 500+ sixes. I thought it was strange, considering that there are 6 numbers and 3000 tosses. So in a "perfect" world there should be 500 counts of each numbers. I got curious and thought I should write a code that runs the main function until there are 500+ counts of sixes and prints out how many times the program ran until it reached 500 sixes. I know how to do it by writing if and else in the main function but Im currently learning about functions so I thought the best way to to it is by using functions only.
this is what I did
The parts i greyed out are the code I wrote that dont work. Please tell me where my thinking went wrong and how I should approach such tasks in the future. I want to improve the way I think when coding.Code:// // main.c // oppgave 3-6 terning // // Created by Aria Nemati on 07/04/2019. // Copyright © 2019 Aria Nemati. All rights reserved. // #include <stdio.h> #include <stdlib.h> #include <time.h> int terning(void); void print(int antall); int main(){ int i; int x; int antall=0; int funksjon=1; printf("kaster en terning 3000 ganger!\n"); srand(time(NULL)); for (i=0; i<3000; i++){ x = terning(); if (x==6){ antall++; } } print(antall); /*while (antall < 440){ main(); funksjon++; } printf("antall ganger programmet er kjørt: %d" , funksjon);*/ return 0; } int terning(void){ int x = rand()%7; return x; } void print(int antall){ printf("antall seksere: %d\n" , antall); }