Is there an equivalent of windows rand_s
function on linux ?
Indeed, a rand
function but generating random number between 0 and UINT_MAX` (4294967295)
I have the solution of combine some digit rand()
to create a big rand but I'm pretty sure the probabilities will no more be 1/UINT_MAX (because rand
is a pseudo-random function and here I calculate with a sequence of rand
call).
For example, the following generates number between 0 and 4000000000:
unsigned int random = (unsigned int)((unsigned int)((rand()%4) * 1000000000) + (rand()%10) * 100000000 + (rand()%10) * 10000000 + (rand()%10) * 1000000 + (rand()%10) * 100000 + (rand()%10) * 10000 + (rand()%10) * 1000 + (rand()%10) * 100 + (rand()%10) * 10 + rand()%10);
Thanks in advance.
Here is a piece of code which works (Thanks a lot Dan)
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int i, randomSrc = open("/dev/urandom", O_RDONLY);
unsigned int random;
for(i=0;i<1000;i++)
{
read(randomSrc , &random, sizeof(unsigned int));
printf("%u\n",random);
}
close(randomSrc);
return 0;
}