]> git.mxchange.org Git - flightgear.git/blob - Lib/Math/fg_random.c
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / Math / fg_random.c
1 // fg_random.c -- routines to handle random number generation
2 //
3 // Written by Curtis Olson, started July 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>         // for random(), srandom()
31 #include <time.h>           // for time() to seed srandom()        
32
33 #include "fg_random.h"
34
35 #ifndef HAVE_RAND
36 #  ifdef sgi
37 #    undef RAND_MAX
38 #    define RAND_MAX 2147483647
39 #  endif
40 #endif
41
42 #ifdef __SUNPRO_CC
43     extern "C" {
44         long int random(void);
45         void srandom(unsigned int seed);
46     }
47 #endif
48
49
50 // Seed the random number generater with time() so we don't see the
51 // same sequence every time
52 void fg_srandom(void) {
53     // fgPrintf( FG_MATH, FG_INFO, "Seeding random number generater\n");
54
55 #ifdef HAVE_RAND
56     srand(time(NULL));
57 #else
58     srandom(time(NULL));
59 #endif                                       
60 }
61
62
63 // return a random number between [0.0, 1.0)
64 double fg_random(void) {
65 #ifdef HAVE_RAND
66     return(rand() / (double)RAND_MAX);
67 #else
68     return(random() / (double)RAND_MAX);
69 #endif
70 }
71
72
73 // $Log$
74 // Revision 1.10  1998/11/07 19:07:03  curt
75 // Enable release builds using the --without-logging option to the configure
76 // script.  Also a couple log message cleanups, plus some C to C++ comment
77 // conversion.
78 //
79 // Revision 1.9  1998/11/06 21:17:26  curt
80 // Converted to new logstream debugging facility.  This allows release
81 // builds with no messages at all (and no performance impact) by using
82 // the -DFG_NDEBUG flag.
83 //
84 // Revision 1.8  1998/04/25 22:06:23  curt
85 // Edited cvs log messages in source files ... bad bad bad!
86 //
87 // Revision 1.7  1998/04/24 00:43:13  curt
88 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
89 //
90 // Revision 1.6  1998/04/18 03:53:42  curt
91 // Miscellaneous Tweaks.
92 //
93 // Revision 1.5  1998/04/03 22:10:29  curt
94 // Converting to Gnu autoconf system.
95 //
96 // Revision 1.4  1998/02/03 23:20:28  curt
97 // Lots of little tweaks to fix various consistency problems discovered by
98 // Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
99 // passed arguments along to the real printf().  Also incorporated HUD changes
100 // by Michele America.
101 //
102 // Revision 1.3  1998/01/27 00:47:59  curt
103 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
104 // system and commandline/config file processing code.
105 //
106 // Revision 1.2  1997/12/30 20:47:48  curt
107 // Integrated new event manager with subsystem initializations.
108 //
109 // Revision 1.1  1997/07/30 16:04:09  curt
110 // Moved random routines from Utils/ to Math/
111 //
112 // Revision 1.1  1997/07/19 22:31:57  curt
113 // Initial revision.
114 //