Random number is not reused

I need to generate and reuse a 5 digit random number every time my program is executed. But the following generates random numbers every time the function is called.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>

int RANDOM(void)
{
    int RANDOMNUMBER = 0;

    srand((unsigned int)time(NULL));
    RANDOMNUMBER = rand() % 99999; // create a random 5 digit number to be reused in other functions

    if (RANDOMNUMBER < 10000)
    {
        RANDOMNUMBER = RANDOMNUMBER + 10000; // correction because sometimes the number generated is 4 digits long
    }

    return RANDOMNUMBER;
}

char *ONE(void)
{
    static char ONE1[15] = "";

    sprintf(ONE1, "ONE%.5i", RANDOM());

    return ONE1;
}

char *TWO(void)
{
    static char TWO2[15] = "";

    sprintf(TWO2, "TWO%.5i", RANDOM());

    return TWO2;
}

void THREE(void)
{
    printf("THREE%.5i\n", RANDOM());
}

int main(void)
{
/* A different random number is output every second. This is a problem because the program takes various seconds to execute.
How can I reuse a random number every time the program is executed ? */

    puts(ONE());
    sleep(1);
    puts(ONE());
    sleep(1);
    puts(ONE());
    sleep(1);
    puts(TWO());
    sleep(1);
    puts(TWO());
    sleep(1);
    puts(TWO());
    sleep(1);
    THREE();
    sleep(1);
    THREE();
    sleep(1);
    THREE();

    return 0;
}

You're reseeding the PRNG every time you're calling your own RANDOM, so it's no wonder you're getting different values every time. If you need just one "random" value, call rand() once per program execution and save the value, or save the seed value and reuse that.

I've been fiddling with this for hours. I've tried to save the random number in a variable outside a function but gcc keeps saying something like that the random number is not constant. Somehow I always end up with a different random number.

If somebody could draw up a quick example I would be grateful.

How did you try to save it? Usually, I'd just allocate one global int, assign it the value, and be done with it. gcc shouldn't complain about that.

gcc error:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>

int RANDOM(void)
{
    int RANDOMNUMBER = 0;

    srand((unsigned int)time(NULL));
    RANDOMNUMBER = rand() % 99999; // create a random 5 digit number to be reused in other functions

    if (RANDOMNUMBER < 10000)
    {
        RANDOMNUMBER = RANDOMNUMBER + 10000; // correction because sometimes the number generated is 4 digits long
    }

    return RANDOMNUMBER;
}

int NUMBER = RANDOM();

char *ONE(void)
{
    static char ONE1[15] = "";

    sprintf(ONE1, "ONE%.5i", NUMBER);

    return ONE1;
}

char *TWO(void)
{
    static char TWO2[15] = "";

    sprintf(TWO2, "TWO%.5i", NUMBER);

    return TWO2;
}

void THREE(void)
{
    printf("THREE%.5i\n", NUMBER);
}

int main(void)
{
/* A different random number is output every second. This is a problem because the program takes various seconds to execute.
How can I reuse a random number every time the program is executed ? */

    puts(ONE());
    sleep(1);
    puts(ONE());
    sleep(1);
    puts(ONE());
    sleep(1);
    puts(TWO());
    sleep(1);
    puts(TWO());
    sleep(1);
    puts(TWO());
    sleep(1);
    THREE();
    sleep(1);
    THREE();
    sleep(1);
    THREE();

    return 0;
}

It's saying it's not constant because it's not. :wink: You can't initialize variables from non-constant things, i.e. functions in C. Initialize it once at the beginning of main().

I tried deleting the RANDOM function and placing its contents inside main, then use a variable to feed the random number to the other functions. This works yet it defeats the whole purpose of using a function to generate a random number.

This is for a website, so having to include this bunch of code inside every single webpage is kind of inefficient... That is why I wanted to use a single function.

Is there no other way of achieving this by reusing the RANDOM function???

Why?

An entire function call is more efficient than retrieving one variable? I think not.

All these attempted solutions effectively do the same thing: Call rand() once, store the value, use it many times. Calling RANDOM() repeatedly is overhead on top of this, not "more efficient".

Why does it have to be so since a function call is more expensive.

You can make the RANDOMNUMBER a static variable and avoid using caps for names...

int RANDOM(void)
{
    srand((unsigned int)time(NULL));

    static int RANDOMNUMBER = rand() % 99999; // create a random 5 digit number to be reused in other functions
    

    if (RANDOMNUMBER < 10000)
    {
        RANDOMNUMBER = RANDOMNUMBER + 10000; // correction because sometimes the number generated is 4 digits long
    }

    return RANDOMNUMBER;
}

No you can't. Same error: initializer is not a constant. That only works in C++.

It works with C source as long as it is compiled with a C++ compiler.

Which is something the OP obviously isn't using.

Nothing stops the OP from using the freeware gcc-g++ compiler.