0

I can't seem to figure out why this is not working. I am trying to get these two void functions to generate randoms numbers when called in the main. I think the problem has to do with srand(time(NULL)); in the void function's. I think that when it gets called in the main and runs in the for loop, srand(time(NULL)); is not updated so it prints out the same number x times. numGen_10 should generate 1-10 and numGen_4 should generate 1-4 based on a variable.

What can I do to get the functions to output different random numbers each time the function is called?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void numGen_10(int xOry, int* xRey);
void numGen_4(int num, int choice, int *choiceRe);


int main()
{
    int x;
    int num = 5;

    for (int i = 0; i < 3; i++)
    {
        numGen_10(x, &x);
        printf("val : %d \n", x);
    }

    printf("------------------------\n");

    for (int i = 0; i < 4; i++)
    {
        numGen_4(4, x, &x);
        printf("val2 : %d \n", x);
    }
    return 0;
}



void numGen_10(int xOry, int *xRey)
{
        srand(time(NULL));
        int r = rand() % 10 + 1;
        xOry = r;
        *xRey = xOry;
    return;
}

void numGen_4(int num, int choice, int *choiceRe)
{
    srand(time(NULL));
    choice = rand() % num + 1;
    *choiceRe = choice;
    return;
}
T.Malo
  • 512
  • 1
  • 7
  • 24
  • 8
    `srand` should only be called once in the entire run of the program. Call it as the first executable statement in main. – ooga Apr 23 '14 at 01:32
  • @ooga: You should probably add that as an answer! :) – Baldrick Apr 23 '14 at 01:33
  • 2
    @Baldrick I'm sure it's been answered many times!.http://stackoverflow.com/questions/7343833/srand-why-call-only-once/7343909#7343909 – ooga Apr 23 '14 at 01:34
  • @ooga Oh... You only call it once? – T.Malo Apr 23 '14 at 01:37
  • @T.Malo That's what you want in your case. By calling it before each `rand` call, and calling it in a tight loop, you end up re-seeding the RNG each time with exactly the same number (since `time` usually only returns seconds), giving you the same result over and over. – ooga Apr 23 '14 at 01:39
  • Wow did not know. Thank you for your help. – T.Malo Apr 23 '14 at 01:41
  • refer to [this](http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand) for better understanding – chouaib Apr 23 '14 at 02:23

3 Answers3

1

The random number generator is completely deterministic; it will always output the same sequence of numbers for any given seed. srand() seeds the random number generator. So by calling srand() with the time immediately before each call to rand(), and since your loops will take less than a second to execute, you'll always get the same random number (or maybe two if the time happens to change in the middle).

Try calling srand() once before entering the loops.

user3553031
  • 5,990
  • 1
  • 20
  • 40
1

Just for fun , add a delay of 1 second before each srand call when calling srand multiple times :

    sleep(1);
    /* or delay(1001); or usleep(500000);usleep(500000); based on availability */
    srand(time(NULL));
Uours
  • 2,517
  • 1
  • 16
  • 21
  • Do you mean 60 iteration loop will take 1 minute to execute the program of few Micro-seconds ? – chouaib Apr 23 '14 at 02:27
  • What library includes delay? – T.Malo Apr 23 '14 at 02:49
  • 1
    @T.Malo If delay is not available , try `sleep(1);` or `usleep(1000000);` as said just for fun ! Both worked for me . My goodness , I compiled a c program after 10+ years . – Uours Apr 23 '14 at 04:35
  • Haha I gather that the industry does not use C but school really wants you to know it. Java, C++, and other higher level seem like what you should really know. – T.Malo Apr 23 '14 at 17:58
  • @T.Malo I'm in industry, and coding in `C` is 60% of the work I do for the company. – chouaib Apr 23 '14 at 23:57
1

Additional to what others said about solving problem of srand I would like to add a remark about how you use pointers, calling your functions with x and &x is pointless, you can just call either with pointer only or with variable only:

calling with pointer :

void numGen_10(int* xRey); // prototype
void numGen_10(int *xRey) // function's body
{
    int r = rand() % 10 + 1;
    *xRey = r; 
    return;
}
numGen_4(4, &x); // use from the main function

calling with variable :

int numGen_10(int xRey); // prototype
int numGen_10(int xRey) //function's body 
{
    int r = rand() % 10 + 1;
    return r;
}
int y = numGen_4(4, x);  // use from the main function
chouaib
  • 2,763
  • 5
  • 20
  • 35