X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=bootcensus;a=blobdiff_plain;f=src%2Flibc%2Frand.c;fp=src%2Flibc%2Frand.c;h=de159c290447408128bbc5c3eae5f9daca8d84f0;hp=0000000000000000000000000000000000000000;hb=78e3e75fc7b5838d0261c876d00e1b8c3e0bcfe0;hpb=7b6f6de2124e28ae7da5599a7cdaf2c171c4f15e diff --git a/src/libc/rand.c b/src/libc/rand.c new file mode 100644 index 0000000..de159c2 --- /dev/null +++ b/src/libc/rand.c @@ -0,0 +1,65 @@ +/* +pcboot - bootable PC demo/game kernel +Copyright (C) 2018-2019 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY, without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +/* random number generator, based on this description of the algorithm + * used by the GNU libc: https://www.mathstat.dal.ca/~selinger/random + */ +#include +#include + +static int init_done; +static int32_t rng[34]; +static int32_t *ptr0, *ptr1; + +int rand(void) +{ + int res; + + if(!init_done) { + srand(1); + } + + *ptr1 += *ptr0; + res = (uint32_t)*ptr1 >> 1; + if(++ptr0 >= rng + 34) ptr0 = rng; + if(++ptr1 >= rng + 34) ptr1 = rng; + + return res; +} + +void srand(unsigned int seed) +{ + int i; + + init_done = 1; + if(seed == 0) seed = 1; + + rng[0] = seed; + for(i=1; i<31; i++) { + rng[i] = (16807 * rng[i - 1]) % RAND_MAX; + if(rng[i] < 0) rng[i] += RAND_MAX; + } + for(i=31; i<34; i++) { + rng[i] = rng[i - 31]; + } + ptr0 = rng + 3; + ptr1 = rng + 31; + + for(i=34; i<344; i++) { + rand(); + } +}