L
luserXtrog
Hello all.
I'm down to the very last math operator for my postscript interpreter,
and it's a doozy.
Postscript defines three operators related to pseudorandom number
generation:
[args] operator [value(s) returned]
-- rand int
int srand --
-- rrand int
I've got rand and srand using the eponymous standard library
functions:
void Orand() {
push(integer(rand()));
}
void Osrand() {
stackis1(integer,srand); /*assert stack content*/
srand((unsigned)pop.u.i); /*pop,offset,cast,call*/
}
But there's no standard library function to discover the current
state. My man page describes a POSIX function,
int rand_r(unsigned int *seedp);
that would let me maintain the seed in a global or something; but it
also gives an example implementation:
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
What's better: steal the code or depend on POSIX?
tia.
I'm down to the very last math operator for my postscript interpreter,
and it's a doozy.
Postscript defines three operators related to pseudorandom number
generation:
[args] operator [value(s) returned]
-- rand int
int srand --
-- rrand int
I've got rand and srand using the eponymous standard library
functions:
void Orand() {
push(integer(rand()));
}
void Osrand() {
stackis1(integer,srand); /*assert stack content*/
srand((unsigned)pop.u.i); /*pop,offset,cast,call*/
}
But there's no standard library function to discover the current
state. My man page describes a POSIX function,
int rand_r(unsigned int *seedp);
that would let me maintain the seed in a global or something; but it
also gives an example implementation:
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
What's better: steal the code or depend on POSIX?
tia.