Close

a rand()... or so.

A project log for PEAC Pisano with End-Around Carry algorithm

Add X to Y and Y to X, says the song. And carry on.

yann-guidon-ygdesYann Guidon / YGDES 6 days ago0 Comments

I have recently received inquiries from two projects :

Both are interested by a new/better implementation of the rand() function and it would be beneficial to merge these two requests.

First, I need a good definition of the requirements : size of the state, width of the registers... I'll first try a typical version with two variables, not much larger than the historic implementation (congruential generators have only one register). I suppose that the variable seed must be adapted...

Already, a limitation : POSIX defines rand() as returning an int limited by RAND_MAX>=32767

The <stdlib.h> header shall define the following macros which shall expand
 to integer constant expressions:
{RAND_MAX}
    Maximum value returned by rand(); at least 32767.

On my local installation, /usr/include/stdlib.h says

/* The largest number rand will return (same as INT_MAX).  */
#define RAND_MAX        2147483647

(Note: yet another reason that POSIX IS DEAD ! You CAN'T know your local size until you test the system, instead of being sure to have a certain width that lets you program comfortably everywhere).
 .
Now, the recent libc has more than one function :

void          srand(unsigned);
void          srand48(long);
void          srandom(unsigned);
....
int           rand(void);
int           rand_r(unsigned *);
long          jrand48(unsigned short [3]);
long          lrand48(void);
long          mrand48(void);
long          nrand48(unsigned short [3]);
long          random(void);
double        drand48(void);
double        erand48(unsigned short [3]);

So it's a whole crazy mess... and https://pubs.opengroup.org/onlinepubs/9699919799/functions/srand.html adds more mess. But who uses it anyway ?

For TrapC,

We need

     crypto_random_t cr;// for casino real-money gaming
     packet_random_t sr;// for encrypted streaming transmission
     pseudo_random_t rr("test 1");// for repeatable A/B testing
     hash32_t h32("hello world");
     hash64_t h64("hello world");

And constructors to automatically do the right thing to seed generators. An application programmer doesn't need to know how they work to use them.

.

Let's go back to POSIX.

Discussions