Article View: comp.lang.eiffel
Article #1094Re: Need a random number generator
From: David Quarrie
Date: Thu, 07 Jan 1993 13:42
Date: Thu, 07 Jan 1993 13:42
101 lines
3070 bytes
3070 bytes
--
In article <1993Jan7.082202.7901@city.cs>, mb108@cs.city.ac.uk (omo
ADELAKUN T K) writes:
|>Hi folks,
|>I need a (pseudo-)random number generator for use in a tester program
|>for a queuing system. The Eiffel libraries don't appear to have such
|>a class - that is, according to grep. I can't see anything that looks
|>like a library of binaries against I can run nm. (I'm working
|>remotely
|>from the labs and have no access to manuals.)
|>
|>If there _is_ one I'd like to know where to find it, and if there
|>isn't
|>one I'd appreciate pointers to public-domain sources (FTP?) of such
|>classes. (I'm being a pedant for software reuse... 8-)
|>
|>
|>Thanx++, -- Oh, dear..! That syntax!
|>Toyin.
|>
I assume you want something that's uniform in the range 0.0-1.0. What I did
was the following:
Create a class (I called it MORE_MATH since it contained some functions
missing from the MATH) class (Eiffel 2.3.4 syntax):
class MORE_MATH export
arctangent, exp, random
feature
exp (x: REAL): REAL is
-- Exponential function of x.
external
c_exp:REAL language "C"
do
Result := c_exp (x)
end; -- exp
arctangent (x: REAL): REAL is
-- Arctangent of x.
external
c_atan:REAL language "C"
do
Result := c_atan (x)
end; -- arctangent
random: REAL is
-- Random Number generator in range 0.0-1.0.
external
c_rndm: REAL language "C"
do
Result := c_rndm
end; -- random
end -- class MORE_MATH
Alongside of this I created a more_math.c file containing:
#include <math.h>
#include <values.h>
float c_atan();
float c_exp(), c_rndm();
float c_atan(x)
float x;
{
return ((float) atan ((double) x));
}
float c_rndm()
{
float f1, f2;
f1 = random();
f2 = MAXINT;
return (f1/f2);
}
float c_exp(x)
float x;
{
return (exp(x));
}
Compile the more_math.c file and create an archive file from it and add that
to the EXTERNALS section of your SDF (.eiffel) file. I'm not sure whether
the C random() function is generic Unix, but I'm fairly sure that rand() is
so you could use that instead.
David
-------------------------------------------------------------------------------
David Quarrie CEBAF MS 12H
12000 Jefferson Ave
Internet: quarrie@cebaf.gov Newport News VA 23606
DECnet : cebaf::quarrie Tel: (804) 249-7562
BITNET : quarrie@cebaf Fax: (804) 249-5800
Technical Details
Message-ID:
<1993Jan7.134255.14773@murdoch.acc.Virginia.EDU>Path:
arc1-ca.pugleaf.net!sparky!uunet!gatech!paladin.american.edu!howland.reston.ans.net!zaphod.mps.ohio-state.edu!malgudi.oar.net!caen!uvaarpa!murdoch!cds001.cebaf.gov!quarrieReferences:
<1993Jan7.082202.7901@city.cs>Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.eiffel Path: arc1-ca.pugleaf.net!sparky!uunet!gatech!paladin.american.edu!howland.reston.ans.net!zaphod.mps.ohio-state.edu!malgudi.oar.net!caen!uvaarpa!murdoch!cds001.cebaf.gov!quarrie From: quarrie@cds001.cebaf.gov (David Quarrie) Subject: Re: Need a random number generator Message-ID: <1993Jan7.134255.14773@murdoch.acc.Virginia.EDU> Sender: usenet@murdoch.acc.Virginia.EDU Reply-To: quarrie@cds001.cebaf.gov (David Quarrie) Organization: CEBAF (Continuous Electron Beam Accelerator Facility) References: <1993Jan7.082202.7901@city.cs> Date: Thu, 7 Jan 1993 13:42:55 GMT Lines: 101