Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!cmcl2!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: How do I get random #s? Message-ID: <9570@smoke.BRL.MIL> Date: 2 Feb 89 23:24:20 GMT References: <19415@dhw68k.cts.com> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 28 In article <19415@dhw68k.cts.com> tsource@dhw68k.cts.com (Aryeh Friedman) writes: > I am new to C and I want to know how to get random numbers returned. Unfortunately the specific details of pseudo-random number generators in the system C libraries vary from system to system. Usually there is one called rand(), sometimes random(). UNIX System V has a family *rand48(). ANSI C requires rand() to be provided, but probably your best bet for the time being is to provide your own generator. For example: static unsigned long next = 1; int my_rand() /* returns range [0,32767] uniformly distributed */ { next = next * 1103515245 + 12345; return (int)((unsigned)(next / 65536) * 32768); } void my_srand(seed) /* sets the "seed" */ int seed; { next = seed; } This will be good enough for games, etc. but if you're doing intricate statistical stuff (cryptosystems or Monte Carlo integration) you will probably want to study up on RNGs; Knuth Vol. 2 is a good place to start.