File: randserv.c

package info (click to toggle)
xscorch 0.2.0-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 4,636 kB
  • ctags: 3,399
  • sloc: ansic: 31,504; sh: 8,826; makefile: 467; perl: 16
file content (115 lines) | stat: -rw-r--r-- 3,097 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* $Header: /fridge/cvs/xscorch/sutil/randserv.c,v 1.3 2003/02/20 11:48:48 jacob Exp $ */
/*--------------------------------------------------------------------
 *
 *  randserv.c -- Random deviate generator
 *  Copyright (C) 2000 Matti Hnninen
 *
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to 
 *
 *    The Free Software Foundation, Inc.
 *    59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <math.h>
#include <stdlib.h>

#include <randserv.h>
#include <srand.h>



static double normal_deviates[2];
static int    normal_deviates_left = 0;

/*--------------------------------------------------------------------
 *  rand_uniform() 
 *
 *  Description:
 *     Returns an uniform deviate drawn from the closed 
 *     interval [0, 1].
 */

inline double rand_uniform(void) 
{
  return (double)game_rand() / (double)GAME_RAND_MAX;
}

/*--------------------------------------------------------------------
 *  rand_discrete(min, max) 
 *
 *  Description:
 *     Return a discrete deviate bounded between min and max.
 */

long rand_discrete(long min, long max)
{
  long temp;

  /* Make sure min is less than max */
  if ( min > max ) {
    temp = min;
    min = max;
    max = temp;
  }
  
  return min + (long)((max - min + 1.0) * game_rand() / (GAME_RAND_MAX + 1.0));
}

/*--------------------------------------------------------------------
 *  generate_normal_deviates() <internal>
 *
 *  Description:
 *     Generates two independent standard normal deviates and 
 *     stores them in the normal_deviates[] table.
 *
 *  Note:
 *     This function utilizes the ``polar method'' for generating
 *     normal deviations due to Box, Muller, and Marsaglia as 
 *     described in Knuth, D. E. (1981) The Art of Computer
 *     Programming, Vol. 2, pp. 117-8.  
 */

static void generate_normal_deviates(void) 
{
  double v_1, v_2, s, t;

  do {
    v_1 = 2.0 * (double)game_rand() / (double)GAME_RAND_MAX - 1.0;
    v_2 = 2.0 * (double)game_rand() / (double)GAME_RAND_MAX - 1.0;
    s = v_1 * v_1 + v_2 * v_2;
  } while ( s >= 1 );
  t = sqrt((-2.0 * log(s)) / s);
  normal_deviates[0] = v_1 * t;
  normal_deviates[1] = v_2 * t;
  normal_deviates_left = 2;
}

/*--------------------------------------------------------------------
 *  rand_normal() 
 *  
 *  Description:
 *     Returns a standard normal deviate.
 */

double rand_normal(void) 
{
  if ( !normal_deviates_left )
    generate_normal_deviates();

  return normal_deviates[--normal_deviates_left]; 
}

/* randserv.c ends */