| 1 | /*BHEADER**********************************************************************
|
|---|
| 2 | * Copyright (c) 2008, Lawrence Livermore National Security, LLC.
|
|---|
| 3 | * Produced at the Lawrence Livermore National Laboratory.
|
|---|
| 4 | * This file is part of HYPRE. See file COPYRIGHT for details.
|
|---|
| 5 | *
|
|---|
| 6 | * HYPRE is free software; you can redistribute it and/or modify it under the
|
|---|
| 7 | * terms of the GNU Lesser General Public License (as published by the Free
|
|---|
| 8 | * Software Foundation) version 2.1 dated February 1999.
|
|---|
| 9 | *
|
|---|
| 10 | * $Revision: 2.4 $
|
|---|
| 11 | ***********************************************************************EHEADER*/
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | /******************************************************************************
|
|---|
| 15 | *
|
|---|
| 16 | * This file contains routines that implement a pseudo-random number generator
|
|---|
| 17 | * detailed in the following paper.
|
|---|
| 18 | *
|
|---|
| 19 | * @article{RNG_Park_Miller,
|
|---|
| 20 | * author = {S. K. Park and K. W. Miller},
|
|---|
| 21 | * title = {Random number generators: good ones are hard to find},
|
|---|
| 22 | * journal = {Commun. ACM},
|
|---|
| 23 | * volume = {31},
|
|---|
| 24 | * number = {10},
|
|---|
| 25 | * year = {1988},
|
|---|
| 26 | * pages = {1192--1201},
|
|---|
| 27 | * }
|
|---|
| 28 | *
|
|---|
| 29 | * This RNG has been shown to appear fairly random, it is a full period
|
|---|
| 30 | * generating function (the sequence uses all of the values available to it up
|
|---|
| 31 | * to 2147483647), and can be implemented on any architecture using 32-bit
|
|---|
| 32 | * integers. The implementation in this file will not overflow for 32-bit
|
|---|
| 33 | * arithmetic, which all modern computers should support.
|
|---|
| 34 | *
|
|---|
| 35 | * @author David Alber
|
|---|
| 36 | * @date March 2005
|
|---|
| 37 | *
|
|---|
| 38 | *****************************************************************************/
|
|---|
| 39 |
|
|---|
| 40 | /*--------------------------------------------------------------------------
|
|---|
| 41 | * Static variables
|
|---|
| 42 | *--------------------------------------------------------------------------*/
|
|---|
| 43 |
|
|---|
| 44 | static int Seed = 13579;
|
|---|
| 45 |
|
|---|
| 46 | #define a 16807
|
|---|
| 47 | #define m 2147483647
|
|---|
| 48 | #define q 127773
|
|---|
| 49 | #define r 2836
|
|---|
| 50 |
|
|---|
| 51 | /*--------------------------------------------------------------------------
|
|---|
| 52 | * Initializes the pseudo-random number generator to a place in the sequence.
|
|---|
| 53 | *
|
|---|
| 54 | * @param seed an int containing the seed for the RNG.
|
|---|
| 55 | *--------------------------------------------------------------------------*/
|
|---|
| 56 |
|
|---|
| 57 | void hypre_SeedRand( int seed )
|
|---|
| 58 | {
|
|---|
| 59 | Seed = seed;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /*--------------------------------------------------------------------------
|
|---|
| 63 | * Computes the next pseudo-random number in the sequence using the global
|
|---|
| 64 | * variable Seed.
|
|---|
| 65 | *
|
|---|
| 66 | * @return a double containing the next number in the sequence divided by
|
|---|
| 67 | * 2147483647 so that the numbers are in (0, 1].
|
|---|
| 68 | *--------------------------------------------------------------------------*/
|
|---|
| 69 |
|
|---|
| 70 | double hypre_Rand()
|
|---|
| 71 | {
|
|---|
| 72 | int low, high, test;
|
|---|
| 73 |
|
|---|
| 74 | high = Seed / q;
|
|---|
| 75 | low = Seed % q;
|
|---|
| 76 | test = a * low - r * high;
|
|---|
| 77 | if(test > 0)
|
|---|
| 78 | {
|
|---|
| 79 | Seed = test;
|
|---|
| 80 | }
|
|---|
| 81 | else
|
|---|
| 82 | {
|
|---|
| 83 | Seed = test + m;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | return ((double)(Seed) / m);
|
|---|
| 87 | }
|
|---|