-2

I'm trying to make a program to generate 2 random numbers and print them to the screen. This is achieved by calling the Numbers function twice and assigning the value to num1 and num2 then calling PrintMsg twice also with those variables but instead the function prints the first value twice.

In the debugger num1 and num2 are being set to 2 different numbers and the mode variable is being successfully passed through to the PrintMsg function.

// C program to generate random numbers 
#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include<time.h> 

int Numbers() {

    bool valid = false;
    int randNum;

    srand(time(0)); 

    while(valid != true) {

        randNum = rand() % 100;

        if (randNum > 0 && randNum <= 6) {
            valid = true;
        } 
    }

    return(randNum);
}

void PrintMsg(int x, int mode) {

    if (mode == 1) {

        switch(x) {

            case 1:
                printf(" %d ", x);
                break;

            case 2:
                printf(" %d ", x);
                break;

            case 3:
                printf(" %d ", x);
                break;

            case 4:
                printf(" %d ", x);
                break;

            case 5:
                printf(" %d ", x);
                break;

            case 6:
                printf(" %d ", x);
                break;
        }
    }

    else if (mode == 2){

        switch(x) {

            case 1:
                printf(" %d ", x);
                break;

            case 2:
                printf(" %d ", x);
                break;

            case 3:
                printf(" %d ", x);
                break;

            case 4:
                printf(" %d ", x);
                break;

            case 5:
                printf(" %d ", x);
                break;

            case 6:
                printf(" %d ", x);
                break;


        return;
    }
}

int main(void) 
{ 
    int num1;
    int num2;

    num1 = Numbers();

    PrintMsg(num1, 1);

    num2 = Numbers();

    PrintMsg(num2, 2);

    return 0; 
}

Thanks.

  • Why do you write the same values both in mode=1 and mode=2? The problem is what @user3121023 indicates. When you use debugger the time value may change 'cause you execute the code lines step by step, but this is not true at runtime. – Sir Jo Black Jul 14 '19 at 13:30
  • They're just test values. – C.Deighton Jul 14 '19 at 13:34

1 Answers1

2

You are reseeding the random number generator with srand() on every call to Numbers(), using the current time as returned by time(). Now time() returns time measured in seconds, so if you call Numbers() twice within the same second, you seed the generator with the same value, and so you get the same values returned by rand().

You should instead call srand(time(0)) only once, at the beginning of your program.

As Sir Jo Black noted in a comment, when you ran the code in the debugger, you probably waited more than one second between successive calls to Number(), which is why you didn't see this behavior in that case.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82