00001 /* $Id: xsh_model_randlcg.c,v 1.6 2011/12/02 14:15:28 amodigli Exp $ 00002 * 00003 * This file is part of the ESO X-shooter Pipeline 00004 * Copyright (C) 2006 European Southern Observatory 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 /* 00022 * $Author: amodigli $ 00023 * $Date: 2011/12/02 14:15:28 $ 00024 * $Revision: 1.6 $ 00025 * $Name: HEAD $ 00026 */ 00027 /* rndlcg Linear Congruential Method, the "minimal standard generator" 00028 Park & Miller, 1988, Comm of the ACM, 31(10), pp. 1192-1201 00029 00030 */ 00031 #ifdef HAVE_CONFIG_H 00032 #include <config.h> 00033 #endif 00034 /*---------------------------------------------------------------------------*/ 00041 /*---------------------------------------------------------------------------*/ 00043 /*----------------------------------------------------------------------------- 00044 Includes 00045 -----------------------------------------------------------------------------*/ 00046 00047 #include <cpl.h> 00048 00049 #include "xsh_model_kernel.h" 00050 00051 /*----------------------------------------------------------------------------*/ 00052 00053 //static char rcsid[] = "@(#)xsh_randlcg.c 1.1 15:48:15 11/21/94 EFC"; 00054 00055 #include <math.h> 00056 #include <limits.h> 00057 #include <xsh_model_randlcg.h> 00058 00059 #define ALL_BITS 0xffffffff 00060 00061 static long int the_quotient = LONG_MAX / 16807L; 00062 static long int the_remainder = LONG_MAX % 16807L; 00063 00064 static long int seed_val = 1L; 00065 00066 long xsh_set_seed(long int sd) 00067 { 00068 return seed_val = sd; 00069 } 00070 00071 long get_seed(void) 00072 { 00073 return seed_val; 00074 } 00075 00076 00077 unsigned long int xsh_randlcg(void) /* returns a random unsigned integer */ 00078 { 00079 if ( seed_val <= the_quotient ) 00080 seed_val = (seed_val * 16807L) % LONG_MAX; 00081 else 00082 { 00083 long int high_part = seed_val / the_quotient; 00084 long int low_part = seed_val % the_quotient; 00085 00086 long int test = 16807L * low_part - the_remainder * high_part; 00087 00088 if ( test > 0 ) 00089 seed_val = test; 00090 else 00091 seed_val = test + LONG_MAX; 00092 00093 } 00094 00095 return seed_val; 00096 } 00097