www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/03/07:25:57

From: "Ike Gingerich" <drone AT ismi DOT net>
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
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 <stdlib.h>
#include <time.h>

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2014.210" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; 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.&nbsp; This bothered me =
becase for=20
games I wrote the random maps, powerups, ect. usually always came up the =
same=20
way.&nbsp; I wrote some code to fix this,&nbsp; and I will post it =
here.&nbsp;=20
</FONT></DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; Generally how it works is it gets =
the=20
current time in milliseconds from the system timer and devides that =
number by=20
4,&nbsp; then executes that many random number generations before =
assigning the=20
real one to the variable in the argument.&nbsp; Its really speedy on my =
P200MMX=20
but I imagine that it may be a real calculation hog.&nbsp; Does anyone =
have any=20
suggestions on my code,&nbsp; or possibly an eayser way to do =
this?</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DCourier size=3D2>#include &lt;stdlib.h&gt;<BR>#include =

&lt;time.h&gt;</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DCourier size=3D2>int random_number(int x, int=20
y)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Call the function =
with two=20
arguments:&nbsp;=20
*/<BR>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;=20
/* x=3D lowest number to generate y=3D highest*/</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DCourier size=3D2>&nbsp;&nbsp; struct time=20
{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
/* The standard DJGPP time struct */<BR>&nbsp;&nbsp;&nbsp;&nbsp; =
unsigned char=20
ti_min;<BR>&nbsp;&nbsp;&nbsp;&nbsp; unsigned char=20
ti_hour;<BR>&nbsp;&nbsp;&nbsp;&nbsp; unsigned char=20
ti_hund;<BR>&nbsp;&nbsp;&nbsp;&nbsp; unsigned char =
ti_sec;<BR>&nbsp;&nbsp;=20
}t;<BR>&nbsp;&nbsp; <BR>&nbsp;&nbsp; int i;</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DCourier size=3D2>&nbsp;&nbsp;=20
gettime(&amp;t);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;=20
/* Fill up the time struct */</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DCourier size=3D2>&nbsp;&nbsp; for (i=3D0; i &lt; =
t.ti_hund / 4;=20
i++)&nbsp;&nbsp;&nbsp; /* Devide the current time (milliseconds)=20
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (rand() %=20
0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
/* by 4 and create useless random numbers=20
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp; if (y &gt;=20
x)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp; return ((rand() % (y - x)) + =
x);&nbsp;&nbsp; /*=20
Return a random number between y and x */<BR>&nbsp;&nbsp;=20
else<BR>&nbsp;&nbsp;&nbsp;&nbsp; return ((rand() % (x - y)) + =
y);&nbsp;&nbsp; /*=20
or x and y, as the case may be */<BR>}<BR></FONT></DIV></BODY></HTML>

------=_NextPart_000_000A_01BE7D06.33E56520--

- Raw text -


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