global variable `CGAL_random' added

This commit is contained in:
Sven Schönherr 1997-05-09 14:13:27 +00:00
parent 0681511a1a
commit cff990223e
3 changed files with 76 additions and 53 deletions

View File

@ -29,21 +29,24 @@ It can be very usefull, e.g.\ for debugging, to reproduce a sequence
of random numbers. This can be done using the seed functions of random numbers. This can be done using the seed functions
described below. described below.
\ccInclude{CGAL/Random.h} \ccInclude{CGAL/Random.h}
\ccCreationVariable{random}
% -----------------------------------------------------------------------------
\ccHeading{Global Variables}
\ccVariable{ CGAL_Random CGAL_random;}{
This global variable is used as the
default random numbers generator.}
% ----------------------------------------------------------------------------- % -----------------------------------------------------------------------------
\ccTypes \ccTypes
\ccSetThreeColumns{double}{\ccVar.restore_seed( Seed seed)}{}
\ccPropagateThreeToTwoColumns
\ccUnchecked \ccUnchecked
\ccNestedType{Seed}{Seed type.} \ccNestedType{Seed}{Seed type.}
% ----------------------------------------------------------------------------- % -----------------------------------------------------------------------------
\ccCreation \ccCreation
\ccCreationVariable{random}
\ccConstructor{ CGAL_Random( );}{ \ccConstructor{ CGAL_Random( );}{
introduces a variable \ccVar\ of type \ccClassTemplateName.} introduces a variable \ccVar\ of type \ccClassTemplateName.}

View File

@ -29,21 +29,24 @@ It can be very usefull, e.g.\ for debugging, to reproduce a sequence
of random numbers. This can be done using the seed functions of random numbers. This can be done using the seed functions
described below. described below.
\ccInclude{CGAL/Random.h} \ccInclude{CGAL/Random.h}
\ccCreationVariable{random}
% -----------------------------------------------------------------------------
\ccHeading{Global Variables}
\ccVariable{ CGAL_Random CGAL_random;}{
This global variable is used as the
default random numbers generator.}
% ----------------------------------------------------------------------------- % -----------------------------------------------------------------------------
\ccTypes \ccTypes
\ccSetThreeColumns{double}{\ccVar.restore_seed( Seed seed)}{}
\ccPropagateThreeToTwoColumns
\ccUnchecked \ccUnchecked
\ccNestedType{Seed}{Seed type.} \ccNestedType{Seed}{Seed type.}
% ----------------------------------------------------------------------------- % -----------------------------------------------------------------------------
\ccCreation \ccCreation
\ccCreationVariable{random}
\ccConstructor{ CGAL_Random( );}{ \ccConstructor{ CGAL_Random( );}{
introduces a variable \ccVar\ of type \ccClassTemplateName.} introduces a variable \ccVar\ of type \ccClassTemplateName.}

View File

@ -28,8 +28,6 @@
\RCSdef{\rcsrevision}{$Revision$} \RCSdef{\rcsrevision}{$Revision$}
\RCSdefDate{\rcsdate}{$Date$} \RCSdefDate{\rcsdate}{$Date$}
@!thickline
@t vskip 5 mm @t vskip 5 mm
@t title titlefont centre "CGAL -- Random Numbers Generator*" @t title titlefont centre "CGAL -- Random Numbers Generator*"
@t vskip 1 mm @t vskip 1 mm
@ -39,7 +37,6 @@
\smallskip \smallskip
\centerline{\rcsrevision\ , \rcsdate} \centerline{\rcsrevision\ , \rcsdate}
@t vskip 1 mm @t vskip 1 mm
@!thickline
\renewcommand{\thefootnote}{\fnsymbol{footnote}} \renewcommand{\thefootnote}{\fnsymbol{footnote}}
\footnotetext[1]{This work was supported by the ESPRIT IV LTR Project \footnotetext[1]{This work was supported by the ESPRIT IV LTR Project
@ -64,33 +61,35 @@ performs some correctness checks. Finally the product files are
created in Section~4. created in Section~4.
\tableofcontents \tableofcontents
\clearpage
@! ============================================================================ @! ============================================================================
@! Specification @! Specification
@! ============================================================================ @! ============================================================================
\clearpage
\section{Specification} \section{Specification}
\renewcommand{\ccSection}{\ccSubsection} \renewcommand{\ccSection}{\ccSubsection}
\renewcommand{\ccFont}{\tt} \renewcommand{\ccFont}{\tt}
\renewcommand{\ccEndFont}{} \renewcommand{\ccEndFont}{}
\ccSetThreeColumns{CGAL_Random}{random.restore_seed( Seed seed)}{}
\ccPropagateThreeToTwoColumns
\input{../spec/Random.tex} \input{../spec/Random.tex}
\clearpage
@! ============================================================================ @! ============================================================================
@! Implementation @! Implementation
@! ============================================================================ @! ============================================================================
\clearpage
\section{Implementation} \section{Implementation}
This section describes the implementation of the random numbers This section describes the implementation of the random numbers
generator. We use the C library function @prg{erand48} to generate the generator. We use the C library function \ccc{erand48} to generate the
random numbers. It behaves like the well-known function @prg{drand48} but random numbers. It behaves like the well-known function \ccc{drand48} but
operates on a user supplied storage for the 48-Bit seed. This makes different operates on a user supplied storage for the 48-Bit seed. This makes different
instances of the random number generator independent. instances of the random number generator independent.
First, we declare the class @prg{CGAL_Random}. First, we declare the class \ccc{CGAL_Random}.
@macro<Random declaration> = @begin @macro<Random declaration> = @begin
class CGAL_Random; class CGAL_Random;
@ -135,9 +134,23 @@ section, so we do not comment on it here.
\pagebreak \pagebreak
\subsection{Global Variable}
The global variable \ccc{CGAL_random} is the default random numbers
generator.
@macro <Random global variable declaration> = @begin
extern CGAL_Random CGAL_random;
@end
@macro <Random global variable definition> = @begin
CGAL_Random CGAL_random;
@end
\subsection{Private Data Members} \subsection{Private Data Members}
The seed is stored in an array of three @prg{unsigned short}s. The seed is stored in an array of three \ccc{unsigned short}s.
@macro <Random private data members> = @begin @macro <Random private data members> = @begin
// data members // data members
@ -180,8 +193,8 @@ time.
\subsection{Operations} \subsection{Operations}
The C library function @prg{erand48} returns a random @prg{double}, The C library function \ccc{erand48} returns a random \ccc{double},
uniformly chosen from the interval $[@prg{0.0},@prg{1.0})$. uniformly chosen from the interval $[\ccc{0.0},\ccc{1.0})$.
The result is converted to a number in the given range. The result is converted to a number in the given range.
@macro <Random operations> = @begin @macro <Random operations> = @begin
@ -220,12 +233,12 @@ The result is converted to a number in the given range.
@end @end
\subsection{Seed Operations} \subsection{Seed Functions}
The seed operations just copy the internal seed to or from the given The seed functions just copy the internal seed to or from the given
seed variable, respectively. seed variable, respectively.
@macro <Random seed operations> = @begin @macro <Random seed functions> = @begin
void void
CGAL_Random:: CGAL_Random::
save_seed( Seed& seed) const save_seed( Seed& seed) const
@ -249,55 +262,55 @@ seed variable, respectively.
@! Test @! Test
@! ============================================================================ @! ============================================================================
\clearpage
\section{Test} \section{Test}
We call each function of class @prg{CGAL_Random} at least once to We call each function of class \ccc{CGAL_Random} at least once to
ensure code coverage. In addition, we check if the generated random ensure code coverage. In addition, we check if the generated random
numbers lie in the given ranges, and if two random numbers generators numbers lie in the given ranges, and if two random numbers generators
initialized with the same seed generate the same sequence of random initialized with the same seed generate the same sequence of random
numbers. numbers.
@macro <Random tests> = @begin @macro <Random tests> = @begin
CGAL_Random rnd;
CGAL_Random::Seed seed; CGAL_Random::Seed seed;
rnd.save_seed( seed); CGAL_random.save_seed( seed);
// test get_bool // test get_bool
{ {
bool b = rnd.get_bool(); bool b = CGAL_random.get_bool();
CGAL_assertion( ! b || b); assert( ! b || b);
} }
// test get_int // test get_int
{ {
int l = rnd.get_int( -100, 0); int l = CGAL_random.get_int( -100, 0);
int u = rnd.get_int( 0, 1000); int u = CGAL_random.get_int( 0, 1000);
int i = rnd.get_int( l, u); int i = CGAL_random.get_int( l, u);
CGAL_assertion( ( l <= i) && ( i < u)); assert( ( l <= i) && ( i < u));
} }
// test get_double // test get_double
{ {
double l = rnd.get_double( -123.45, -0.99); double l = CGAL_random.get_double( -123.45, -0.99);
double u = rnd.get_double( 22.0/7.0, 33.3); double u = CGAL_random.get_double( 22.0/7.0, 33.3);
double d = rnd.get_double( l, u); double d = CGAL_random.get_double( l, u);
CGAL_assertion( ( l <= d) && ( d < u)); assert( ( l <= d) && ( d < u));
} }
// test operator() // test operator()
{ {
int i = rnd( 5555); int i = CGAL_random( 5555);
CGAL_assertion( ( 0 <= i) && ( i < 5555)); assert( ( 0 <= i) && ( i < 5555));
} }
// test seed functions // test seed functions
{ {
rnd.restore_seed( seed); // now `rnd' and `rnd2' CGAL_random.restore_seed( seed); // `CGAL_Random' and `rnd'
CGAL_Random rnd2( seed); // have the same seed CGAL_Random rnd( seed); // have the same seed now
CGAL_assertion( rnd.get_bool() == rnd2.get_bool()); assert( CGAL_random.get_bool() == rnd.get_bool());
CGAL_assertion( rnd.get_int( -100, 100) == rnd2.get_int( -100, 100)); assert( CGAL_random.get_int( -100,100) == rnd.get_int( -100,100));
CGAL_assertion( rnd.get_double() == rnd2.get_double()); assert( CGAL_random.get_double() == rnd.get_double());
} }
@end @end
@ -305,8 +318,7 @@ numbers.
@! Files @! Files
@! ========================================================================== @! ==========================================================================
\pagebreak \clearpage
\section{Files} \section{Files}
@file <Random.h> = @begin @file <Random.h> = @begin
@ -328,6 +340,10 @@ numbers.
@<Random interface> @<Random interface>
// Global variables
// ================
@<Random global variable declaration>
@<dividing line> @<dividing line>
// Class implementation (inline functions) // Class implementation (inline functions)
@ -350,20 +366,21 @@ numbers.
// constructors // constructors
@<Random constructors> @<Random constructors>
// ssed operations // seed functions
@<Random seed operations> @<Random seed functions>
// Global variables
// ================
@<Random global variable definition>
@<end of file line> @<end of file line>
@end @end
\pagebreak
@file <test_Random.C> = @begin @file <test_Random.C> = @begin
@<Random header>("test/test_Random.C") @<Random header>("test/test_Random.C")
#include <CGAL/Random.h> #include <CGAL/Random.h>
#include <assert.h>
#define CGAL_assertion CGAL_kernel_assertion
int int
main( int, char**) main( int, char**)