From: "Ike Gingerich" Newsgroups: comp.os.msdos.djgpp Subject: DJGPP's random functions. Date: Fri, 2 Apr 1999 12:42:00 -0800 Lines: 145 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_000A_01BE7D06.33E56520" X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2014.211 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211 NNTP-Posting-Host: tc3-26.ismi.net Message-ID: <3705018e@news.ismi.net> X-Trace: 2 Apr 1999 12:42:38 -0500, tc3-26.ismi.net To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com This is a multi-part message in MIME format. ------=_NextPart_000_000A_01BE7D06.33E56520 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I noticed that the functions that come with DJGPP for generating = random numbers always spit out the random numbers in the same sequence = each time the program is run. This bothered me becase for games I wrote = the random maps, powerups, ect. usually always came up the same way. I = wrote some code to fix this, and I will post it here. =20 Generally how it works is it gets the current time in milliseconds = from the system timer and devides that number by 4, then executes that = many random number generations before assigning the real one to the = variable in the argument. Its really speedy on my P200MMX but I imagine = that it may be a real calculation hog. Does anyone have any suggestions = on my code, or possibly an eayser way to do this? #include #include int random_number(int x, int y) /* Call the function with two = arguments: */ { /* x=3D lowest number to = generate y=3D highest*/ struct time { /* The standard DJGPP time = struct */ unsigned char ti_min; unsigned char ti_hour; unsigned char ti_hund; unsigned char ti_sec; }t; =20 int i; gettime(&t); /* Fill up the time struct */ for (i=3D0; i < t.ti_hund / 4; i++) /* Devide the current time = (milliseconds) */ (rand() % 0); /* by 4 and create useless = random numbers */ =20 if (y > x) =20 return ((rand() % (y - x)) + x); /* Return a random number = between y and x */ else return ((rand() % (x - y)) + y); /* or x and y, as the case may = be */ } ------=_NextPart_000_000A_01BE7D06.33E56520 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
    I noticed that the functions that = come with=20 DJGPP for generating random numbers always spit out the random numbers = in the=20 same sequence each time the program is run.  This bothered me = becase for=20 games I wrote the random maps, powerups, ect. usually always came up the = same=20 way.  I wrote some code to fix this,  and I will post it = here. =20
    Generally how it works is it gets = the=20 current time in milliseconds from the system timer and devides that = number by=20 4,  then executes that many random number generations before = assigning the=20 real one to the variable in the argument.  Its really speedy on my = P200MMX=20 but I imagine that it may be a real calculation hog.  Does anyone = have any=20 suggestions on my code,  or possibly an eayser way to do = this?
 
#include <stdlib.h>
#include = <time.h>
 
int random_number(int x, int=20 y)         /* Call the function = with two=20 arguments: =20 */
{           =             &= nbsp;           &n= bsp;  =20 /* x=3D lowest number to generate y=3D highest*/
 
   struct time=20 {            =            =20 /* The standard DJGPP time struct */
     = unsigned char=20 ti_min;
     unsigned char=20 ti_hour;
     unsigned char=20 ti_hund;
     unsigned char = ti_sec;
  =20 }t;
  
   int i;
 
  =20 gettime(&t);         &nb= sp;           &nbs= p;  =20 /* Fill up the time struct */
 
   for (i=3D0; i < = t.ti_hund / 4;=20 i++)    /* Devide the current time (milliseconds)=20 */
      (rand() %=20 0);           &nbs= p;        =20 /* by 4 and create useless random numbers=20 */
           &= nbsp;           &n= bsp;           &nb= sp;  =20
   if (y >=20 x)            = ;            =   =20
     return ((rand() % (y - x)) + = x);   /*=20 Return a random number between y and x */
  =20 else
     return ((rand() % (x - y)) + = y);   /*=20 or x and y, as the case may be */
}
------=_NextPart_000_000A_01BE7D06.33E56520--