Skip to content

Instantly share code, notes, and snippets.

@berenoguz
Last active April 10, 2016 20:44
Show Gist options
  • Save berenoguz/a710b60f4a2fd2737fa3 to your computer and use it in GitHub Desktop.
Save berenoguz/a710b60f4a2fd2737fa3 to your computer and use it in GitHub Desktop.
Implementing Mersenne Twister 32 Algorithm for C++ templates
/**
* @challenge Implement Mersenne Twister 32 algorithm by only using C++ template variables
* @see https://gist.github.com/berenoguz/eae088a41f71696a7c11
* @usage You can use random integers for your templates (see above gist), so that behavior of templates will change every compilation
* @author Beren Oguz
* @copyright Copyright (c) 2015, Beren Oguz
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
template<class Type, Type seed, int count>
constexpr Type mtseed = (0x6c078965UL*((mtseed<Type,seed,count-1>)^((mtseed<Type,seed,count-1>)>>30))+count)&0xffffffffUL;
template<class Type, Type seed>
constexpr Type mtseed<Type,seed,0> = seed & 0xffffffffUL;
template<class Type,int n>
constexpr Type mersenne_mask = 0x9908b0dfUL;
template<class Type> constexpr Type mersenne_mask<Type,0> = 0;
template<class Type, Type seed, int count,bool first_half = (count<624-397),bool not_last = (count!=623)>
constexpr Type mtseed2 = 0;
template<class Type, Type seed>
constexpr Type mtseed2<Type,seed,0,true,true> = mtseed<Type,seed,397> ^(((mtseed<Type,seed,0&0x80000000UL>)|(mtseed<Type,seed,1>&0x7fffffffUL))>>1) ^mersenne_mask<Type,((mtseed<Type,seed,0&0x80000000UL>)|(mtseed<Type,seed,1>&0x7fffffffUL)) &0x01>;
template<class Type, Type seed, int count>
constexpr Type mtseed2<Type,seed,count,true,true> = mtseed<Type,seed,count+397> ^ (((mtseed2<Type,seed,count&0x80000000UL>)|(mtseed<Type,seed,count+1>&0x7fffffffUL))>>1) ^ mersenne_mask<((mtseed<Type,seed,count&0x80000000UL>)|(mtseed<Type,seed,count+1>&0x7fffffffUL)) &0x01>;
template<class Type, Type seed, int count>
constexpr Type mtseed2<Type,seed,count,false,true> = mtseed2<Type,seed,count+397-624> ^ (((mtseed2<Type,seed,count&0x80000000UL>)|(mtseed2<Type,seed,count+1>&0x7fffffffUL))>>1) ^ mersenne_mask<((mtseed2<Type,seed,count&0x80000000UL>)|(mtseed2<Type,seed,count+1>&0x7fffffffUL)) &0x01>;
template<class Type, Type seed>
constexpr Type mtseed2<Type,seed,623,false,false> = mtseed2<Type,seed,396> ^ (((mtseed2<Type,seed,623> & 0x80000000UL)|(mtseed2<Type,seed,0> & 0x7fffffffUL))>>1) ^ 0x9908b0dfUL;
template<class Type, Type seed, int count>
constexpr Type mtp1 = (mtseed2<Type,seed,count> >> 11)^mtseed2<Type,seed,count>;
template<class Type, Type seed, int count>
constexpr Type mtp2 = ((mtp1<Type,seed,count><<7)&0x9d2c5680UL)^mtp1<Type,seed,count>;
template<class Type, Type seed, int count>
constexpr Type mtp3 = ((mtp2<Type,seed,count><<15)&0xefc60000UL)^mtp2<Type,seed,count>;
template<class Type, Type seed, int count>
constexpr Type mtp4 = (mtp3<Type,seed,count> >> 18)^mtp3<Type,seed,count>;
template<class Type, Type seed, int count>
constexpr Type MersenneTwister32 = mtp4<Type,seed,count>;
template<unsigned seed>
constexpr unsigned mt_rand = MersenneTwister32<unsigned,seed,0>;
int main()
{
std::cout << mt_rand<4234>; /// Any random number to be used as seed
/// #include "compilation_timestamp.hpp"
/// std::cout << mt_rand<COMPILATION_TIMESTAMP>; //This code will be different in each compilation
/// //Check https://gist.github.com/berenoguz/eae088a41f71696a7c11
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment