00001 /* {{{ 00002 * CalcRogue, a roguelike game for PCs, calculators and PDAs 00003 * Copyright (C) 2003 Jim Babcock 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 * }}} */ 00019 // rand.c 00022 00023 #include "crogue.h" 00024 00025 //{{{ 00026 // High-byte is maximum 00027 // Low-byte is minimum 00028 uchar xrandom(uint range) 00029 { 00030 return nrandom(range>>8, range & 0x00FF); 00031 } 00032 //}}} 00033 //{{{ 00034 uchar rrandom(range r) 00035 { 00036 return lrand()%(r.max-r.min+1) + r.min; 00037 } 00038 //}}} 00039 //{{{ 00040 sint nrandom(sint max, sint min) 00041 { 00042 return lrand()%(max-min+1) + min; 00043 } 00044 //}}} 00045 00046 static unsigned long seed; 00047 00048 //{{{ 00049 // Seed the random number generator 00050 void lsrand(unsigned long s) 00051 { 00052 seed = s; 00053 } 00054 //}}} 00055 //{{{ 00056 // Generate a random number in [0,LONG_MAX] 00057 unsigned long lrand(void) 00058 { 00059 seed *= 1103515245; 00060 seed += 12345; 00061 return (seed ^ (seed>>16)) & 0x7FFFFFFF; 00062 } 00063 //}}} 00064 //{{{ 00065 // Given a random number, use it to make the random number generator more 00066 // random. The number given does not have to be perfectly (or even mostly) 00067 // random; anything unpredictable will do. 00068 void feed_entropy(unsigned long entropy) 00069 { 00070 seed += entropy; 00071 lrand(); 00072 } 00073 //}}} 00074
1.3.6