X-shooter Pipeline Reference Manual 3.8.15
xsh_model_randlcg.c
Go to the documentation of this file.
1/* $Id: xsh_model_randlcg.c,v 1.6 2011-12-02 14:15:28 amodigli Exp $
2 *
3 * This file is part of the ESO X-shooter Pipeline
4 * Copyright (C) 2006 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: amodigli $
23 * $Date: 2011-12-02 14:15:28 $
24 * $Revision: 1.6 $
25 * $Name: not supported by cvs2svn $
26 */
27/* rndlcg Linear Congruential Method, the "minimal standard generator"
28 Park & Miller, 1988, Comm of the ACM, 31(10), pp. 1192-1201
29
30*/
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
43/*-----------------------------------------------------------------------------
44 Includes
45 -----------------------------------------------------------------------------*/
46
47#include <cpl.h>
48
49#include "xsh_model_kernel.h"
50
51/*----------------------------------------------------------------------------*/
52
53//static char rcsid[] = "@(#)xsh_randlcg.c 1.1 15:48:15 11/21/94 EFC";
54
55#include <math.h>
56#include <limits.h>
57#include <xsh_model_randlcg.h>
58
59#define ALL_BITS 0xffffffff
60
61static long int the_quotient = LONG_MAX / 16807L;
62static long int the_remainder = LONG_MAX % 16807L;
63
64static long int seed_val = 1L;
65
66long xsh_set_seed(long int sd)
67{
68 return seed_val = sd;
69}
70
71long get_seed(void)
72{
73 return seed_val;
74}
75
76
77unsigned long int xsh_randlcg(void) /* returns a random unsigned integer */
78{
79 if ( seed_val <= the_quotient )
80 seed_val = (seed_val * 16807L) % LONG_MAX;
81 else
82 {
83 long int high_part = seed_val / the_quotient;
84 long int low_part = seed_val % the_quotient;
85
86 long int test = 16807L * low_part - the_remainder * high_part;
87
88 if ( test > 0 )
89 seed_val = test;
90 else
91 seed_val = test + LONG_MAX;
92
93 }
94
95 return seed_val;
96}
97
long get_seed(void)
static long int seed_val
long xsh_set_seed(long int sd)
static long int the_quotient
unsigned long int xsh_randlcg(void)
static long int the_remainder