X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Frand.c;h=40d782448421c4e1201fe31b7dd97aff3c16d94c;hb=d1954049a406af2992113a783539aa5d86cfdfa8;hp=967ae2fcff1e24b5653c15a3830198285db3658a;hpb=e5456e3c2697c4e17fc9aa3439f2e305517b4d96;p=pdclib.old diff --git a/functions/stdlib/rand.c b/functions/stdlib/rand.c index 967ae2f..40d7824 100644 --- a/functions/stdlib/rand.c +++ b/functions/stdlib/rand.c @@ -1,28 +1,35 @@ -// ---------------------------------------------------------------------------- -// $Id$ -// ---------------------------------------------------------------------------- -// Public Domain C Library - http://pdclib.sourceforge.net -// This code is Public Domain. Use, modify, and redistribute at will. -// ---------------------------------------------------------------------------- +/* $Id$ */ -int rand( void ) { /* TODO */ }; -void srand( unsigned int seed ) { /* TODO */ }; +/* rand( void ) -/* PDPC code - unreviewed -static unsigned long myseed = 1; + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include -void srand(unsigned int seed) +#ifndef REGTEST + +int rand( void ) { - myseed = seed; - return; + _PDCLIB_seed = _PDCLIB_seed * 1103515245 + 12345; + return (int)( _PDCLIB_seed / 65536 ) % 32768; } -int rand(void) +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) { - int ret; - - myseed = myseed * 1103515245UL + 12345; - ret = (int)((myseed >> 16) & 0x8fff); - return (ret); + int rnd1, rnd2; + TESTCASE( ( rnd1 = rand() ) < RAND_MAX ); + TESTCASE( ( rnd2 = rand() ) < RAND_MAX ); + srand( 1 ); + TESTCASE( rand() == rnd1 ); + TESTCASE( rand() == rnd2 ); + return TEST_RESULTS; } -*/ + +#endif