Sure...I can always post more code. This is only one part of a much larger project, but I've already gone through and checked to see if this function was called anywhere else or if the prototype exists in any other headers. It doesn't yet, but it probably will eventually, so I haven't made it a static function yet.
In random.h
PHP Code:
//// Includes ///////////////////////////////////////////////////////////
#include <ctime>
//// Prototypes /////////////////////////////////////////////////////////
void new_srandom(unsigned long initial_seed = 0);
static unsigned long new_random();
int rand_number(int from, int to);
int dice(int num, int size);
//// Constants ///////////////////////////////////////////////////////////
static const unsigned long m = 2147483647;
static const unsigned long q = 127773;
static const unsigned int a = 16807;
static const unsigned int r = 2836;
//// Globals ///////////////////////////////////////////////////////////
static unsigned long seed;
In random.cpp
PHP Code:
#include "random.h"
//// Definitions ///////////////////////////////////////////////////////
void new_srandom(unsigned long initial_seed)
{
seed = (initial_seed == 0) ? (unsigned)time(NULL) : initial_seed;
}
unsigned long new_random()
{
int lo, hi, test;
hi = seed/q;
lo = seed%q;
test = a*lo - r*hi;
if (test > 0)
seed = test;
else
seed = test+ m;
return (seed);
}
/* creates a random number in interval [from;to] */
int rand_number(int from, int to)
{
/* error checking in case people call this incorrectly */
if (from > to) {
int tmp = from;
from = to;
to = tmp;
//inform of error
}
return ((new_random() % (to - from + 1)) + from);
}
/* simulates dice roll */
int dice(int num, int size)
{
int sum = 0;
if (size <= 0 || num <= 0)
return (0);
while (num-- > 0)
sum += rand_number(1, size);
return (sum);
}
Much of this code is already public domain. I don't know if it works. If it does, I'm not sure why it works. All I'm trying to do is put a default argument on new_srandom().
BTW, I typed it in wrong the first time. The function is new_srandom(), not new_random().