mercredi 6 décembre 2017

How do I write helper function in C to return a random number from a specific range and avoid a segmentation fault?

I'm attempting to write a program in which a helper function returns a random integer between -999 and 999 and returns it for use in the main function.

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


int rand(void) {
    int n;
    n = ((rand() % 1998 + 1) - 999);
    return n;
}


int main() {
    srand(time(NULL));     /* seed the random number generator */
    printf("%d\n",rand());

    return 0;
}

However, despite many different tweaks and simplifications of the main function to debug at bare bones, I'm still getting a segmentation fault.

What am I missing? What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire