File: UnsyncedRNG.h

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (57 lines) | stat: -rw-r--r-- 1,681 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef UNSYNCEDRNG_H_
#define UNSYNCEDRNG_H_

#include "System/float3.h"

// like 5-6x slower than our, but much better distribution
//#define USE_BOOST_RNG
#ifdef USE_BOOST_RNG
	#include <boost/random/mersenne_twister.hpp>
	#include <boost/random/uniform_01.hpp>
	#include <boost/random/uniform_smallint.hpp>
	#include <boost/random/uniform_on_sphere.hpp>
	#include <boost/random/variate_generator.hpp>
#endif


class UnsyncedRNG
{
public:
	UnsyncedRNG();
	void Seed(unsigned seed);
	
	/** @brief returns a random unsigned integer in the range [0, (UINT_MAX & 0x7FFF)) */
	unsigned operator()() { int r = RandInt(); return *reinterpret_cast<unsigned*>(&r); }
	
	/** @brief returns a random integer in the range [0, (INT_MAX & 0x7FFF)) */
	int RandInt();

	/** @brief returns a random float in the range [0, 1) */
	float RandFloat();
	
	/** @brief returns a random vector with length [0, 1] */
	float3 RandVector();
	float3 RandVector2D();
	
	/** @brief returns a random number in the range [0, n) */
	int operator()(int n);
	void operator=(const UnsyncedRNG& urng);

private:
	unsigned randSeed;

#ifdef USE_BOOST_RNG
	boost::random::mt19937 rng;
	boost::random::uniform_01<float> dist01;
	boost::random::uniform_smallint<int> distInt;
	boost::random::uniform_on_sphere<float> distSphere;
	boost::variate_generator<boost::random::mt19937&, boost::uniform_01<float> > gen01;
	boost::variate_generator<boost::random::mt19937&, boost::uniform_smallint<int> > genInt;
	boost::variate_generator<boost::random::mt19937&, boost::uniform_on_sphere<float> > genSphere;
#endif
};

#endif // UNSYNCEDRNG_H_