Swap code/variable declaration to be pre-C99 compliant.
[xiph/unicode.git] / planarity / random.c
blob9d4b2258e045aa281d1dcb7ba1bd1c7eec475dda
1 /*
3 * gPlanarity:
4 * The geeky little puzzle game with a big noodly crunch!
5 *
6 * gPlanarity copyright (C) 2005 Monty <monty@xiph.org>
7 * Original Flash game by John Tantalo <john.tantalo@case.edu>
8 * Original game concept by Mary Radcliffe
10 * gPlanarity is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
15 * gPlanarity is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Postfish; see the file COPYING. If not, write to the
22 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "random.h"
29 // Portable 32 bit random number generator. It's not crypto-grade,
30 // but we don't need crypto-grade. We need complete control over the
31 // result sequence and reproducability, so we can't use any local
32 // generator. As long as it's 100% consistent across
33 // platforms/OSes/compilers and fairly uniform (doesn't always return
34 // 17), we're all set.
36 // This is a C derivative of the PASCAL "Integer Version 2" minimal
37 // standard number generator thich appears in the article:
38 // Park, Steven K. and Miller, Keith W., "Random Number
39 // Generators: Good Ones are Hard to Find", Communications of the
40 // ACM, October, 1988.
43 static int32_t next = 123456789;
45 void random_seed(int32_t seed){
46 next = seed;
49 #define MPLIER 16807
50 #define MOBYMP 127773
51 #define MOMDMP 2836
53 int32_t random_number(){
54 int32_t hival, loval, testval;
56 hival = next / MOBYMP;
57 loval = next % MOBYMP;
58 testval = MPLIER*loval - MOMDMP*hival;
59 if (testval > 0)
60 next = testval;
61 else
62 next = testval + MAX_G_RAND;
64 return next;
67 int random_yes(int per128_yes){
68 u_int32_t num = (u_int32_t)random_number();
69 return (num < (unsigned int)per128_yes << 24U);