delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/01/03:16:34

From: "Steve Patton" <leaphe AT pemail DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: random numbers
Date: Thu, 1 Jan 1998 01:00:58 -0700
Organization: AT&T WorldNet Services
Lines: 69
Message-ID: <68figv$1cd@bgtnsc02.worldnet.att.net>
References: <34AB3DFB DOT 25A AT hotmail DOT com>
NNTP-Posting-Host: 12.67.32.197
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Well I belive random() returns a value between 0 and MAX_INT and rand()
returns a value between 0 and RAND_MAX, which may be the same value, I'm not
sure, you'd have to look it up.  You will not get a truely "random" number
until you use srand( int seed), so I suggest making a header file (could be
rand.h) and placing the following inline functions into it

**************SNIP HERE**********
#include <time.h>     // just to make sure it's defined


__inline__ void randomize ( void )
 {
  srand ( time(0) );
 }

__inline__ int getrand ( int min, int max )
 {
  return (( rand() % (max-min+1))+min);
 }

******************END SNIP**************

that is a pretty accurate, getrand will return a value between and including
min and max..so if you do

getrand ( 0, 3 );

it will return a possible 0, 1, 2, or 3...  this (incase you're interested
in knowing how it works, and you don't already know), rand() will return a
value between 0 and something... it doesn't really matter what it is,
because it's a BIG top number (2 billion probably), but the % operator
divides the first number by the second number and returns the remainder.. so
say A is a random number, and you do A % 4, the possible remainders are 0,
1, 2, and 3, but NOT 4...which is why we add one, so A % (4+1) will return
0->4, but that will only give you 0-x, if you want a min number, you must
subtract min from max, before you add 1, and MOD it by A,  so if you want a
random number between (and including) 2 and 8, this will subtract 2 from 8,
which gives you 6, add one, which is 7, which will return a value 0->6...
which is not your number range, but you add min after that, which returns
0->8.  Thus you get a valid range.  This of course is not an extremely
efficient way of ranging it, because it assumes any range (I believe it
works with negative numbers, but not totally sure, so try it).  If you are
just wanting a value 0->6, just do (rand() % 7), that way it's a fixed
number, and does not do any extra subtracting or addition, this will save
you one or two clock cycles, not worth it if you're only doing it a few
times, but if you were doing it in say something that gets called like 100
or more times within a short period of time, it might be worth your while to
optimize it yourself.  Probably more info than you wanted, but better more
than less.

-Steve

Dantalion wrote in message <34AB3DFB DOT 25A AT hotmail DOT com>...
>Ok, I have a little problem here... it seems I can't get DJGPP to
>generate a random number. I tried using something like srandom(34) then
>using r=random() and printf("%d", r), but it would always give me a
>large six or more digit number, and when I use rand() it would always
>give me a zero. Say I wanted a random number from 1 to 10, how would I
>do it? And also, how can I make it so that srandom takes a different
>seed every time the program is run, so that I get a different number
>each time?
>
>Thanks for any help you can give.
>
>--
>Suddenly, I heard a tapping... as of someone gently rapping, rapping at
>my chamber door. You heard me rapping, right?  --The Crow


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019