RE: [AD] Pending patches request |
[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
> Maybe it's time to add the "al_rand(int min, int max)" function to
> Allegro?
I'm using my "own" version, adapted from the K&R RNG (from "The C
Programming Language"). It also uses an external seed, which is nice for
multiple paths of number generation. The rand()%256 "striping" issue is
apparent in this RNG as well, but can be overcome (if one must use a power
of two modulus) by using the float version (fmrand()*256):
// mrand.h
#define MRAND_MAX 0xFFFF
typedef unsigned long int SEED;
int mrand(SEED *seed); // Returns an int between 0-65536 (0 >= n <
65536)
float fmrand(SEED *seed); // Returns a float between 0 and 1.0 (0 >= n
< 1.0)
// mrand.c
#include "mrand.h"
int mrand(SEED *seed)
{
*seed = (*seed+1) * 1103515245 + 12345;
return (unsigned int)((*seed)>>16)%(MRAND_MAX+1);
}
float fmrand(SEED *seed)
{
*seed = (*seed+1) * 1103515245 + 12345;
return ((float)(((*seed)>>16)%(MRAND_MAX+1)))/(MRAND_MAX+1);
}