From: ajm AT algonet DOT se (Anders Musikka) Subject: SV: Random number generator 8 Jan 1998 16:43:40 -0800 Message-ID: <199801081850.KAA07043.cygnus.gnu-win32@cygnus.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_01BD1C6E.9945A320" Content-Transfer-Encoding: 7bit To: "Erric Gilbert" , "'BMullenber'" , Detta är ett multipart-meddelande i MIME-format. ------=_NextPart_000_01BD1C6E.9945A320 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit I've included a very simple (but working) random number generator. The sourcecode is translated pascal code from the book "Algorithms" by Robert Sedgewick. Hope it helps! (also hope that including files is okay on this mailing list? (it's 1.5kb)) ---------- Från: Erric Gilbert Till: 'BMullenber' ; gnu-win32 AT cygnus DOT com Ämne: RE: Random number generator Datum: den 7 januari 1998 22:41 I don't know of any number generators for C/C++ but there is a built in $RANDOM function into BASH. You might be able to incorperate that some how. > -----Original Message----- > From: BMullenber [SMTP:BMullenber AT aol DOT com] > Sent: Wednesday, January 07, 1998 3:05 AM > To: gnu-win32 AT cygnus DOT com > Subject: Random number generator > > Anyone have the source code for a random number generator that > actually works? > > I am using C++ > > I am attempting to get random numbers between 0-100 for a game I wish > to make > that requires some sort of randomness with values. > > If anyone has 1 that can do this, please send it or contact me > > Thanks > > Brandon > BMullenber AT aol DOT com > - > For help on using this list (especially unsubscribing), send a message > to > "gnu-win32-request AT cygnus DOT com" with one line of text: "help". - For help on using this list (especially unsubscribing), send a message to "gnu-win32-request AT cygnus DOT com" with one line of text: "help". ------=_NextPart_000_01BD1C6E.9945A320 Content-Type: application/octet-stream; name="random.c" Content-Transfer-Encoding: quoted-printable Content-Description: random.c (C -fil) Content-Disposition: attachment; filename="random.c" /* Program: very simple Linear Congruential Random number generator. Author: Anders Musikka (M6 Project) notes: See the bottom of this file for some notes about this very = simple number generator. (Naturally, I'm not claiming copyright over this short program...) */ #include #define B 21 #define M 100 int GetRandom() { static seed=3D0; /*It's very important that "seed" is defined = as static here.*/ seed=3D (seed * B + 1) % M; return seed; }; void main() { int i; for (i=3D0;i<100;i++) printf("%d\n",GetRandom()); }; /* B: Should be a random number ending in 21, and = with the number before 21 even. It should have = approximately one less digit than M. good choices: 82621 21 bad choices: 22222221 - Not very random digits before. 1852721 - Uneven number before 21. M: Any number about 1 digit longer than B. A = good suggestion is using a power of 2, as this = makes it possible for the compiler to optimize the % (modulo) operation to an and mask. You can also use the computer-word size - simply remove the % operation. Literature: "Algorithms" by Robert Sedgewick ISBN 0-201-06672-6 */ ------=_NextPart_000_01BD1C6E.9945A320-- - For help on using this list (especially unsubscribing), send a message to "gnu-win32-request AT cygnus DOT com" with one line of text: "help".