delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/28/07:30:23

From: Terence Ramos <de-bug AT ix DOT netcom DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random numbers
Date: Tue, 28 Jan 1997 01:16:51 -0800
Organization: Netcom
Lines: 52
Message-ID: <32EDC403.6621@ix.netcom.com>
References: <5ck9gf$ft7$1 AT fizban DOT solace DOT mh DOT se>
NNTP-Posting-Host: sjx-ca68-18.ix.netcom.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Nils-Erik Svang=E5rd wrote:
> =

> I resently downloaded DJGPP and was thrilled.
> But I have some questions.
> *How do I get the random numbers within my own limit?
>         I currently use this code:
> =

>         while(1)
>         {
>                 tt=3Drandom();
>                 tt=3Dtt/10000000;
>                 if(tt<320)break;
>         }
>         List[loop]=3Dtt;
> =

I still don't know what your doing there but here is my way of returning
random number within my own limits.

first theres this function called rand();

int number;
number =3D rand() % 100;

^^^^^^^^^^^^^^^^^^^^^^<<-- this producues a number from 0 to 99

now pretend you wanted a number from 25 to 35.  First you subtract the
high number from the low number 35 - 25 =3D 10... so =


number =3D rand() % 10;

that produces a number from 0 to 9, but we want 25 to 35... so what you
do is add 25.  25 + random number from 0 to 9.  WAIT! that only goes
does 25 to 34... so.. just add 1 !!! :)  so the whole thing put together
is

int number;

number =3D (rand() % (highNUM-lowNUM)) + lowNUM + 1;

got it... i hope that helps with the random number question you have...
or either that I have no idea what Im talking about and have totally a
whole bunch of mistakes about random number and I am about to get flamed
about my stupidity :)

later

[Terence Ramos      ]
[de-bug AT ix DOT netom DOT com]

- Raw text -


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