00001 /* -*- mode:C++; c-basic-offset:4 -*- 00002 Shore-MT -- Multi-threaded port of the SHORE storage manager 00003 00004 Copyright (c) 2007-2009 00005 Data Intensive Applications and Systems Labaratory (DIAS) 00006 Ecole Polytechnique Federale de Lausanne 00007 00008 All Rights Reserved. 00009 00010 Permission to use, copy, modify and distribute this software and 00011 its documentation is hereby granted, provided that both the 00012 copyright notice and this permission notice appear in all copies of 00013 the software, derivative works or modified versions, and any 00014 portions thereof, and that both notices appear in supporting 00015 documentation. 00016 00017 This code is distributed in the hope that it will be useful, but 00018 WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS 00020 DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER 00021 RESULTING FROM THE USE OF THIS SOFTWARE. 00022 */ 00023 00024 // -*- mode:c++; c-basic-offset:4 -*- 00025 /*<std-header orig-src='shore'> 00026 00027 $Id: rand48.h,v 1.3 2010/07/07 20:50:12 nhall Exp $ 00028 00029 SHORE -- Scalable Heterogeneous Object REpository 00030 00031 Copyright (c) 1994-99 Computer Sciences Department, University of 00032 Wisconsin -- Madison 00033 All Rights Reserved. 00034 00035 Permission to use, copy, modify and distribute this software and its 00036 documentation is hereby granted, provided that both the copyright 00037 notice and this permission notice appear in all copies of the 00038 software, derivative works or modified versions, and any portions 00039 thereof, and that both notices appear in supporting documentation. 00040 00041 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY 00042 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS 00043 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND 00044 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 00045 00046 This software was developed with support by the Advanced Research 00047 Project Agency, ARPA order number 018 (formerly 8230), monitored by 00048 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518. 00049 Further funding for this work was provided by DARPA through 00050 Rome Research Laboratory Contract No. F30602-97-2-0247. 00051 00052 */ 00053 00054 #include "w_defines.h" 00055 00056 /* -- do not edit anything above this line -- </std-header>*/ 00057 00058 /* 00059 * Thread-local pseudo-random-number generator 00060 */ 00061 00062 #include "w_base.h" 00063 #define RAND48_INITIAL_SEED 0x330eabcd1234ull 00064 #define RAND48_INITIALIZER { RAND48_INITIAL_SEED } 00065 00066 typedef w_base_t::int8_t signed48_t; 00067 typedef w_base_t::uint8_t unsigned48_t; 00068 00069 // used by testers (in tests/ and smsh). Not operators because that would conflict 00070 // with the std:: operators for unsigned ints, alas. 00071 #include <fstream> 00072 void in(ifstream& i, unsigned48_t& what); 00073 void out(ofstream& o, const unsigned48_t& what); 00074 00075 /**\brief 48-bit pseudo-random-number generator. 00076 */ 00077 class rand48 { 00078 public: 00079 /// Set the seed 00080 void seed(unsigned48_t seed) { _state = _mask(seed); } 00081 /// Return 48-bit pseudo-random number 00082 signed48_t rand() { return signed48_t(_update()); } 00083 /// Return 64-bit pseudo-random number 00084 double drand(); 00085 00086 /// Return 48-bit pseudo-random number modulo given maximum 00087 signed48_t randn(signed48_t max) { return signed48_t(max*drand()); } 00088 00089 // private: Must be public for test programs. 00090 unsigned48_t _update(); 00091 unsigned48_t _mask(unsigned48_t x) const; 00092 00093 public: 00094 /*! Making _state private * makes this a non-pod type, 00095 * and we use it for thread-private data. 00096 */ 00097 00098 /* No constructor: must seed it. If you get a purify UMR here, 00099 * it's because the struct wasn't seeded, which is a programmer error 00100 */ 00101 00102 unsigned48_t _state; 00103 }; 00104