• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

sst/elements/genericProc/FE/rand.h

00001 // Copyright 2007 Sandia Corporation. Under the terms
00002 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
00003 // Government retains certain rights in this software.
00004 // 
00005 // Copyright (c) 2007, Sandia Corporation
00006 // All rights reserved.
00007 // 
00008 // This file is part of the SST software package. For license
00009 // information, see the LICENSE file in the top level directory of the
00010 // distribution.
00011 
00012 
00013 #ifndef SIM_RAND_H
00014 #define SIM_RAND_H
00015 
00016 #define RAND_MAX_NUM 0x7fffffff
00017 
00018 namespace SimRand {
00019   static int __attribute__((unused))
00020   do_rand(unsigned long *ctx)
00021   {
00022     /*
00023      * Compute x = (7^5 * x) mod (2^31 - 1)
00024      * wihout overflowing 31 bits:
00025      *      (2^31 - 1) = 127773 * (7^5) + 2836
00026      * From "Random number generators: good ones are hard to find",
00027      * Park and Miller, Communications of the ACM, vol. 31, no. 10,
00028      * October 1988, p. 1195.
00029      */
00030     long hi, lo, x;
00031     
00032     /* Can't be initialized with 0, so use another value. */
00033     if (*ctx == 0)
00034       *ctx = 123459876;
00035     hi = *ctx / 127773;
00036     lo = *ctx % 127773;
00037     x = 16807 * lo - 2836 * hi;
00038     if (x < 0)
00039       x += 0x7fffffff;
00040     return ((*ctx = x) % ((unsigned long)RAND_MAX_NUM + 1));
00041   }
00042 
00043   extern unsigned long next;
00044 
00045   static unsigned int __attribute__((unused)) rand() { return do_rand(&SimRand::next); }
00046 };
00047 
00048 #endif

Generated on Fri Oct 22 2010 11:02:22 for SST by  doxygen 1.7.1