I want to generate pseudo-random integers in a given range without introducing the skew that results from the use of rand()%N
.
I have read about the functions random()
and randomize()
that seem to substitute the rand()
and srand()
functions but returning directly an integer in the range given as the parameter of the random()
function. In both cases, the functions seem to be in the stdlib.h
library.
The problem I have is that I cannot make these functions work somehow. Here's a small test code I made to test the functions.
#include <stdio.h>
#include <stdlib.h>
int main(){
randomize();
printf("%d\n",random(100));
return 0;
}
At compilation with gcc -o test test.c
it gives an error
test.c: In function ‘main’:
test.c:6: error: too many arguments to function ‘random’
As far as I know the function random()
only takes one argument which is an integer to determine the range of the numbers given. What am I doing wrong?
EDIT: It seems that those correspond to some TurboC old things. So the question is now, how to make "truly" random integers in the sense that they are not skewed? My approach is (as suggested by Vatine)
#include <stdio.h>
#include <stdlib.h>
int main(){
srand(time(NULL));
printf("%d\n",rand()/(RAND_MAX/100));
return 0;
}
which seems to yield a correct result. Is this adding some bias or the results have at least equal probability of falling into any of the numbers in the range?