aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/common/include/ApexRand.h
blob: 5e83e7272aea615a550cb45061e23dc95a7b8db4 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (c) 2008-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */


#ifndef APEX_RAND_H
#define APEX_RAND_H

#include "PxMath.h"
#include "PxVec3.h"
#include "ApexUsingNamespace.h"

namespace nvidia
{
namespace apex
{

// "Quick and Dirty Symmetric Random number generator"	- returns a uniform deviate in [-1.0,1.0)
class QDSRand
{
	uint32_t mSeed;

public:

	PX_CUDA_CALLABLE PX_INLINE QDSRand(uint32_t seed = 0) : mSeed(seed) {}

	PX_CUDA_CALLABLE PX_INLINE void setSeed(uint32_t seed = 0)
	{
		mSeed = seed;
	}

	PX_CUDA_CALLABLE PX_INLINE uint32_t seed() const
	{
		return mSeed;
	}

	PX_CUDA_CALLABLE PX_INLINE uint32_t nextSeed()
	{
		mSeed = mSeed * 1664525L + 1013904223L;
		return mSeed;
	}

	PX_CUDA_CALLABLE PX_INLINE float getNext()
	{
		union U32F32
		{
			uint32_t   u;
			float   f;
		} r;
		r.u = 0x40000000 | (nextSeed() >> 9);
		return r.f - 3.0f;
	}

	PX_CUDA_CALLABLE PX_INLINE float getScaled(const float min, const float max)
	{
		const float scale = (max - min) / 2.0f;
		return ((getNext() + 1.0f) * scale) + min;
	}

	PX_CUDA_CALLABLE PX_INLINE PxVec3 getScaled(const PxVec3& min, const PxVec3& max)
	{
		return PxVec3(getScaled(min.x, max.x), getScaled(min.y, max.y), getScaled(min.z, max.z));
	}

	PX_CUDA_CALLABLE PX_INLINE float getUnit()
	{
		union U32F32
		{
			uint32_t   u;
			float   f;
		} r;
		r.u = 0x3F800000 | (nextSeed() >> 9);
		return r.f - 1.0f;
	}

};

// "Quick and Dirty Normal Random number generator"	- returns normally-distributed values
class QDNormRand
{
	QDSRand mBase;

public:

	PX_CUDA_CALLABLE PX_INLINE QDNormRand(uint32_t seed = 0) : mBase(seed) {}

	PX_CUDA_CALLABLE PX_INLINE void setSeed(uint32_t seed = 0)
	{
		mBase.setSeed(seed);
	}

	PX_CUDA_CALLABLE PX_INLINE uint32_t setSeed() const
	{
		return mBase.seed();
	}

	PX_CUDA_CALLABLE PX_INLINE uint32_t nextSeed()
	{
		return mBase.nextSeed();
	}

	PX_CUDA_CALLABLE PX_INLINE float getNext()
	{
		//Using Box-Muller transform (see http://en.wikipedia.org/wiki/Box_Muller_transform)

		float u, v, s;
		do
		{
			u = mBase.getNext();
			v = mBase.getNext();
			s = u * u + v * v;
		}
		while (s >= 1.0);

		return u * PxSqrt(-2.0f * PxLog(s) / s);
	}

	PX_CUDA_CALLABLE PX_INLINE float getScaled(const float m, const float s)
	{
		return m + s * getNext();
	}
};

}
} // end namespace nvidia::apex

#endif