crandompthreadssrandusleep

How to do I use a Random seed Generator in a for loop for pthread function usleep()?


I am supposed to be given a number to pass to the thread_mutex initialize function to use as a rondom seed generator for the usleep(). I don't know what that means to create a seed for a random number. The problem I run into when using srand() is that putting the code: srand(timeDelay) in a for loop will make the rand() always the same. The direct instructions say :

This action happens repetitively, until its position is at FINISH_LINE:
//   Randomly calculate a waiting period, no mare than MAX_TIME (rand(3))
//   Sleep for that length of time.
//   Change the display position of this racer by +1 column*:
//     Erase the racer's name from the display.
//     Update the racer's dist field by +1.
//     Display the racer's name at the new position.

code

int main( int argc, char *argv[] ) {
...
long int setDelay = strtol(argv[1], &pEnd, 10);
if ( setDelay == 0 ) {  
        //printf("Conversion failed\n");
        racersNumber = argc-1;
        setDelay = 3;
...
} else {
        //printf("Conversion SSS\n");
        racersNumber = argc-2;
        }
...
initRacers( setDelay);
for (idx = 0; idx < racersNumber; idx++) {
            printf("Thread created %s\n", racersList[idx]);
            //make a racer arra or do I add a pthread make function?
        rc = pthread_create(&threads[idx], NULL, run, (void*) makeRacer(racersList[idx], idx+1));
...

code for init racers to generate "random seed"

static pthread_mutex_t mutex1;
static long int timeDelay;

void initRacers( long distance) {
    printf("Setting the time delay\n");
    //set the mutex

    pthread_mutex_init(&mutex1, NULL);
    timeDelay = distance; 
}

code for run function

#define MAX_TIME 200 // msec
void *run( void *racer ){
    //pre-cond: racer is not NULL
    if ( racer == NULL ) {
        return -1;
    }
    //convert racer back to racer
    for (int col = 0; col < FINISH_LINE; col++ ) {

        Racer *rc;
        //conversion
        rc= (Racer*)racer;
        pthread_mutex_lock(&mutex1);

        if ( col != 0 ) {
            set_cur_pos(rc->row, col-1);
            put(' ');
        }
        set_cur_pos( rc->row, col);
        pthread_mutex_unlock( &mutex1 );
        //loop function for each at same column
        for (int idx = 0; idx < sizeof(rc->graphic); idx++) {
            put(rc->graphic[idx]);
        }

        //srand(timeDelay);
        usleep(100000L*(rand()%timeDelay));

    }
}   

Solution

  • "putting the code: srand(timeDelay) in a for loop". You don't put srand() in a loop. You call it once at the beginning of your program, typically with a time argument, so the random sequence you get from rand() is different each time you run the program.

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main()
    {
        int i;
        srand ((unsigned)time(NULL));
        for (i=0; i<5; i++)
            printf ("%6d", rand());
        printf ("\n");
        return 0;
    }