Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

mt19937rng.cpp

Go to the documentation of this file.
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 #include "mt19937rng.h" // include first
00069 
00070 using namespace sheep;
00071 
00072 /* Period parameters */  
00073 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
00074 #define UMASK 0x80000000UL /* most significant w-r bits */
00075 #define LMASK 0x7fffffffUL /* least significant r bits */
00076 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
00077 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
00078 
00079 
00080 void MT19937RNG::Seed(unsigned long seed) {
00081     int j;
00082     state[0]= seed & 0xffffffffUL;
00083     for (j=1; j<N; j++) {
00084         state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j); 
00085         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
00086         /* In the previous versions, MSBs of the seed affect   */
00087         /* only MSBs of the array state[].                        */
00088         /* 2002/01/09 modified by Makoto Matsumoto             */
00089         state[j] &= 0xffffffffUL;  /* for >32 bit machines */
00090     }
00091     left = 1;
00092 }
00093 
00094 void MT19937RNG::next_state() {
00095     unsigned long *p=state;
00096     int j;
00097 
00098     left = N;
00099     next = state;
00100     
00101     for (j=N-M+1; --j; p++) 
00102         *p = p[M] ^ TWIST(p[0], p[1]);
00103 
00104     for (j=M; --j; p++) 
00105         *p = p[M-N] ^ TWIST(p[0], p[1]);
00106 
00107     *p = p[M-N] ^ TWIST(p[0], state[0]);
00108 }

Generated on Tue May 11 01:31:51 2004 for toxic by doxygen 1.3.6