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