/*****************************************************************************1*2* Elmer, A Finite Element Software for Multiphysical Problems3*4* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland5*6* This library is free software; you can redistribute it and/or7* modify it under the terms of the GNU Lesser General Public8* License as published by the Free Software Foundation; either9* version 2.1 of the License, or (at your option) any later version.10*11* This library is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14* Lesser General Public License for more details.15*16* You should have received a copy of the GNU Lesser General Public17* License along with this library (in file ../LGPL-2.1); if not, write18* to the Free Software Foundation, Inc., 51 Franklin Street,19* Fifth Floor, Boston, MA 02110-1301 USA20*21*****************************************************************************/2223/*******************************************************************************24*25* Random number generator.26*27*******************************************************************************28*29* Author: Juha Ruokolainen30*31* Address: CSC - IT Center for Science Ltd.32* Keilaranta 14, P.O. BOX 40533* 02101 Espoo, Finland34* Tel. +358 0 457 272335* Telefax: +358 0 457 230236* EMail: [email protected]37*38* Date: 30 May 199639*40* Modified by:41*42* Date of modification:43*44******************************************************************************/45/***********************************************************************46|47| URAND.C - Last Edited 6. 8. 198848|49***********************************************************************/5051/*======================================================================52|Syntax of the manual pages:53|54|FUNCTION NAME(...) params ...55|56$ usage of the function and type of the parameters57? explain the effects of the function58= return value and the type of value if not of type int59@ globals effected directly by this routine60! current known bugs or limitations61& functions called by this function62~ these functions may interest you as an alternative function or63| because they control this function somehow64^=====================================================================*/656667/*68* $Id: urand.c,v 1.4 2006/02/07 10:24:44 jpr Exp $69*70* $Log: urand.c,v $71* Revision 1.4 2006/02/07 10:24:44 jpr72* *** empty log message ***73*74* Revision 1.3 2006/02/07 10:21:42 jpr75* Changed visibility of some variables to local scope.76*77* Revision 1.2 2005/05/27 12:26:22 vierinen78* changed header install location79*80* Revision 1.1.1.1 2005/04/14 13:29:14 vierinen81* initial matc automake package82*83* Revision 1.2 1998/08/01 12:34:57 jpr84*85* Added Id, started Log.86*87*88*/8990#include "elmer/matc.h"9192double urand(int *iy)93/*======================================================================94? urand is a uniform random number generator based on theory and95| suggestions given in d.e. knuth (1969), vol 2. the integer iy96| should be initialized to an arbitrary integer prior to the first call97| to urand. the calling program should not alter the value of iy98| between subsequent calls to urand. values of urand will be returned99| in the interval (0,1).100| see forsythe, malcolm and moler (1977).101^=====================================================================*/102{103static double s, halfm;104static int ia, ic, m, mic;105static int m2 = 0, itwo = 2;106#pragma omp threadprivate (s, halfm, ia, ic, m, mic, m2, itwo)107108if (m2 == 0)109{110111/* if first entry, compute machine integer word length */112113m = 1;114do115{116m2 = m;117m = itwo * m2;118} while(m > m2);119halfm = m2;120121/* compute multiplier and increment for linear congruential method */122123ia = 8 * (int)(halfm * atan(1.0) / 8.00) + 5;124ic = 2*(int)(halfm * (0.50 - sqrt(3.0) / 6.0)) + 1;125mic = (m2 - ic) + m2;126127/* s is the scale factor for converting to floating point */128129s = 0.5 / halfm;130131}132133/* compute next random number */134135*iy = *iy * ia;136137/*138the following statement is for computers which do not allow139integer overflow on addition140*/141142if (*iy > mic) *iy = (*iy - m2) - m2;143144*iy = *iy + ic;145146/*147the following statement is for computers where the148word length for addition is greater than for multiplication149*/150151if (*iy / 2 > m2) *iy = (*iy - m2) - m2;152153/*154the following statement is for computers where integer155overflow affects the sign bit156*/157158if (*iy < 0) *iy = (*iy + m2) + m2;159160return *iy * s;161}162163164