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
- a crypto random for crypto stuff,
- a streaming random for telecom stuff,
- a repeatable random for scientific stuff,
- and a good checksum hash that isn't random for hash tables...
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
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.