random.hpp (1141B)
1 /* Relacy Race Detector 2 * Copyright (c) 2008-2013, Dmitry S. Vyukov 3 * All rights reserved. 4 * This software is provided AS-IS with no warranty, either express or implied. 5 * This software is distributed under a license and may not be copied, 6 * modified or distributed except as expressly authorized under the 7 * terms of the license contained in the file LICENSE in this distribution. 8 */ 9 10 #ifndef RL_RANDOM_HPP 11 #define RL_RANDOM_HPP 12 #ifdef _MSC_VER 13 # pragma once 14 #endif 15 16 #include "base.hpp" 17 18 19 namespace rl 20 { 21 22 23 unsigned const primes[16] = {1, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; 24 25 struct random_generator 26 { 27 unsigned k; 28 unsigned c; 29 unsigned x; 30 31 void seed(iteration_t s) 32 { 33 k = ((unsigned)(s >> 32) & 0xf) + 8; 34 c = primes[((unsigned)(s >> 36) & 0xf)]; 35 x = (unsigned)((s + 1) * 0x95949347 + c); 36 } 37 38 unsigned rand() 39 { 40 return ((x = x + c + (x << k)) >> 16); 41 } 42 43 template<typename T, T max> 44 RL_INLINE 45 T get() 46 { 47 return static_cast<T>(rand() % max); 48 } 49 }; 50 51 52 53 } 54 55 #endif