00001 /* 00002 Sheep - A Rigid Body Dynamics Engine 00003 Copyright (C) 2001-2004 Francois Beaune 00004 Contact: http://toxicengine.sourceforge.net/ 00005 00006 This file is part of Sheep. 00007 00008 Sheep is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 Sheep is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with Sheep; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 /* 00024 A C-program for MT19937, with initialization improved 2002/2/10. 00025 Coded by Takuji Nishimura and Makoto Matsumoto. 00026 This is a faster version by taking Shawn Cokus's optimization, 00027 Matthe Bellew's simplification, Isaku Wada's real version. 00028 00029 Before using, initialize the state by using init_genrand(seed) 00030 or init_by_array(init_key, key_length). 00031 00032 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, 00033 All rights reserved. 00034 00035 Redistribution and use in source and binary forms, with or without 00036 modification, are permitted provided that the following conditions 00037 are met: 00038 00039 1. Redistributions of source code must retain the above copyright 00040 notice, this list of conditions and the following disclaimer. 00041 00042 2. Redistributions in binary form must reproduce the above copyright 00043 notice, this list of conditions and the following disclaimer in the 00044 documentation and/or other materials provided with the distribution. 00045 00046 3. The names of its contributors may not be used to endorse or promote 00047 products derived from this software without specific prior written 00048 permission. 00049 00050 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00051 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00052 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00053 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00054 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00055 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00056 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00057 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00058 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00059 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00060 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00061 00062 00063 Any feedback is very welcome. 00064 http://www.math.keio.ac.jp/matumoto/emt.html 00065 email: matumoto@math.keio.ac.jp 00066 */ 00067 00068 inline 00069 MT19937RNG::MT19937RNG(unsigned long seed /*= 5489UL*/) { 00070 Seed(seed); 00071 } 00072 00073 inline 00074 unsigned long MT19937RNG::RandomInt() { 00075 unsigned long y; 00076 00077 if (--left == 0) next_state(); 00078 y = *next++; 00079 00080 /* Tempering */ 00081 y ^= (y >> 11); 00082 y ^= (y << 7) & 0x9d2c5680UL; 00083 y ^= (y << 15) & 0xefc60000UL; 00084 y ^= (y >> 18); 00085 00086 return y; 00087 } 00088 00089 inline 00090 double MT19937RNG::RandomReal1() { 00091 unsigned long y; 00092 00093 if (--left == 0) next_state(); 00094 y = *next++; 00095 00096 /* Tempering */ 00097 y ^= (y >> 11); 00098 y ^= (y << 7) & 0x9d2c5680UL; 00099 y ^= (y << 15) & 0xefc60000UL; 00100 y ^= (y >> 18); 00101 00102 return (double)y * (1.0/4294967295.0); 00103 /* divided by 2^32-1 */ 00104 } 00105 00106 inline 00107 double MT19937RNG::RandomReal2() { 00108 unsigned long y; 00109 00110 if (--left == 0) next_state(); 00111 y = *next++; 00112 00113 /* Tempering */ 00114 y ^= (y >> 11); 00115 y ^= (y << 7) & 0x9d2c5680UL; 00116 y ^= (y << 15) & 0xefc60000UL; 00117 y ^= (y >> 18); 00118 00119 return (double)y * (1.0/4294967296.0); 00120 /* divided by 2^32 */ 00121 } 00122 00123 inline 00124 long MT19937RNG::RandomInt(long a, long b) { 00125 assert(b >= a); 00126 return a + static_cast<long>(RandomReal2() * (b - a + 1)); 00127 } 00128 00129 inline 00130 double MT19937RNG::RandomReal1(double a, double b) { 00131 assert(b >= a); 00132 return a + RandomReal1() * (b - a); 00133 } 00134 00135 inline 00136 double MT19937RNG::RandomReal2(double a, double b) { 00137 assert(b > a); 00138 return a + RandomReal2() * (b - a); 00139 }
1.3.6