]> git.mxchange.org Git - simgear.git/blob - simgear/math/sg_random.c
Patch from Melchior Franz:
[simgear.git] / simgear / math / sg_random.c
1 // sg_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 library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <simgear_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 "sg_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();
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 sg_srandom_time() {
53 #ifdef HAVE_RAND
54     srand(time(NULL));
55 #else
56     srandom(time(NULL));
57 #endif                                       
58 }
59
60
61 // Seed the random number generater with your own seed so can set up
62 // repeatable randomization.
63 void sg_srandom( unsigned int seed ) {
64 #ifdef HAVE_RAND
65     srand( seed );
66 #else
67     srandom( seed );
68 #endif                                       
69 }
70
71
72 // return a random number between [0.0, 1.0)
73 double sg_random() {
74 #ifdef HAVE_RAND
75     return(rand() / (double)RAND_MAX);
76 #else
77     return(random() / (double)RAND_MAX);
78 #endif
79 }
80
81