Sync usage with man page.
[netbsd-mini2440.git] / gnu / lib / libg++ / g++-include / RNG.h
blob414b57f3afe01c78ecab0d9e704da84d7d0a545b
1 // This may look like C code, but it is really -*- C++ -*-
2 /*
3 Copyright (C) 1988 Free Software Foundation
4 written by Dirk Grunwald (grunwald@cs.uiuc.edu)
6 This file is part of GNU CC.
8 GNU CC is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY. No author or distributor
10 accepts responsibility to anyone for the consequences of using it
11 or for whether it serves any particular purpose or works at all,
12 unless he says so in writing. Refer to the GNU CC General Public
13 License for full details.
15 Everyone is granted permission to copy, modify and redistribute
16 GNU CC, but only under the conditions described in the
17 GNU CC General Public License. A copy of this license is
18 supposed to have been given to you along with GNU CC so you
19 can know your rights and responsibilities. It should be in a
20 file named COPYING. Among other things, the copyright notice
21 and this notice must be preserved on all copies.
23 #ifndef _RNG_h
24 #define _RNG_h 1
25 #ifdef __GNUG__
26 #pragma once
27 #pragma interface
28 #endif
30 #include <assert.h>
31 #include <math.h>
33 union PrivateRNGSingleType { // used to access floats as unsigneds
34 float s;
35 unsigned long u;
38 union PrivateRNGDoubleType { // used to access doubles as unsigneds
39 double d;
40 unsigned long u[2];
44 // Base class for Random Number Generators. See ACG and MLCG for instances.
46 class RNG {
47 static PrivateRNGSingleType singleMantissa; // mantissa bit vector
48 static PrivateRNGDoubleType doubleMantissa; // mantissa bit vector
49 public:
50 RNG();
52 // Return a long-words word of random bits
54 virtual unsigned long asLong() = 0;
55 virtual void reset() = 0;
57 // Return random bits converted to either a float or a double
59 float asFloat();
60 double asDouble();
63 #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
66 inline float RNG::asFloat()
68 PrivateRNGSingleType result;
69 result.s = 1.0;
70 result.u |= (asLong() & singleMantissa.u);
71 result.s -= 1.0;
72 assert( result.s < 1.0 && result.s >= 0);
73 return( result.s );
76 inline double RNG::asDouble()
78 PrivateRNGDoubleType result;
79 result.d = 1.0;
80 result.u[0] |= (asLong() & doubleMantissa.u[0]);
81 result.u[1] |= (asLong() & doubleMantissa.u[1]);
82 result.d -= 1.0;
83 assert( result.d < 1.0 && result.d >= 0);
84 return( result.d );
87 #endif
88 #endif