-1

I am wondering why I wont get random numbers. My code is a game in which you roll a dice and then depending on the number you add oder remove smth. That works so far! Now i want to simulate this game like 100 times. But here I always get the same results. I have looked through some "random" Posts and I would assume that I understood how rand works, but couldnt find any solution. Hope you can help me.

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

int items[] = {10,10,10,10,9};

int roll_dice(){
  return rand() % 6;
}
void pick(int index){
  if(items[index] > 0){
    --items[index];
  }
}
bool won(){
  return items[0] == 0 && items[1] == 0 && items[2] == 0 && items[3] == 0;
}
bool lost(){
  return items[4] == 0;
}
void print_config(){
  printf("%u, %u, %u, %u; %u\n", items[0], items[1], items[2], items[3], items[4]);
}
int fullest_basket(){ // Returns index of fullest basket.
  int fullest_basket = 0;
  int index_basket = 0;
  for(int i = 0; i < 4; i++){
    if(fullest_basket < items[i]){
      fullest_basket = items[i];
      index_basket = i;
    }
  }
  return index_basket;
}
void handle_basket(){
  for(int i = 0; i < 2; ++i){
    int basket =  fullest_basket();
    if(items[basket] > 0){
      --items[basket];
    }
  }
}
int main(){
  srand(time(NULL));
  int won_games = 0, lost_games = 0;
  for(int i = 0; i < 100; i++){
    while(1) {
      int rolled = roll_dice(); // return rand() % 6;
      if(rolled < 5){
        pick(rolled);
      } else if( rolled == 5){
        handle_basket();
      }
      if(won()){
        won_games++;
        //printf("WON!\n");
        break;
      }
      if(lost()){
        lost_games++;
        //printf("LOST!\n");
        break;
      }
    }
    //print_config();
  }
  printf("%u, %u; %.2f", won_games, lost_games, (float)won_games/(won_games+lost_games));
  return 0;
}
corken
  • 19
  • 6
  • The source code in the question is missing `#include `. When I fix that and run the program, I get varying results as long as the runs are separated by more than a second (enough time for the value of `time(NULL)` to change). So the problem is not reproducible. It is up to you to present a [mre]. In addition to actually reproducing the problem, it should be minimal. That means you should strip the program down to the minimal amount of code needed to show the behavior that is not random but should be. – Eric Postpischil Dec 11 '20 at 01:45
  • You are not playing the game 100 times, your 2nd game starts with the result of the first, and you dont print the result of the first game. This is the problem with global variables. – Matheus Rossi Saciotto Dec 11 '20 at 02:06
  • Ah I see. So my items[] is actually empty after the first call. And because of that I just win all other games aswell. So I just need to fill items[] back up or better make it local? – corken Dec 11 '20 at 12:19
  • So I fixed it now by refilling items[] after the for loop in main. ```void fill_basket(){ for(int i = 0; i < 4; i++){ items[i] = 10; } items[4] = 9; }``` – corken Dec 11 '20 at 12:41

1 Answers1

1

You are seeding the random generator correctly. You assume that your results are constant and blame it on the generator but the dice results generated are actually correct.

To verify randomness try printing the dice and you will see different numbers every time:

int roll_dice() {
    int x = rand() % 6;
    printf("%d ", x);
    return x;
}

Depending on what you are trying to achieve, if the results are not what you expect then the error must be in the game logic.

RumburaK
  • 1,985
  • 1
  • 21
  • 30