How to generate random numbers in c





How to generate random numbers in c
C code to generate random numbers 


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
    int num;

    //initialize the random number generator
    randomize();

    //Generating the random number
    num = random(99999);

    //Printing the random number
    printf("%d",num);
    getch();
    return 0;
}

Note: random function random(val) generates any number between 1 to num -1 . randomize() function is used only to initialize the random number generator. It internally call the used the header file time.h




1 comment:

Anonymous said...

:11:5: error: too many arguments to function ‘random’
num = random(99999);

:2:0:
/usr/include/stdlib.h:321:17: note: declared here
extern long int random (void) __THROW;
^