Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
RandomTable.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------------------
2Copyright (c) 2006 John Judnich
3
4This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
5Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
61. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
72. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
83. This notice may not be removed or altered from any source distribution.
9-------------------------------------------------------------------------------------*/
10
11#ifndef __RandomTable_H__
12#define __RandomTable_H__
13
14#include <OgrePrerequisites.h>
15#include <OgreMath.h>
16
17#include <random>
18#ifdef _WIN32
19#define _WIN32_WINNT 0x0600
20#include <windows.h>
21#endif
22
23// random table class that speeds up PG a bit
25{
26public:
27 RandomTable(unsigned long size=0x8000);
29
30 void resetRandomIndex();
31 float getUnitRandom();
32 float getRangeRandom(float start, float end);
33
34protected:
35 unsigned long tableSize;
36 float *table;
37 unsigned long customRandomIndex;
38
40};
41
42
43// implementation below
44inline RandomTable::RandomTable(unsigned long size) : tableSize(size), table(0), customRandomIndex(0)
45{
46 table = (float *)malloc(sizeof(float) * tableSize);
48}
49
51{
52 if(table)
53 {
54 free(table);
55 table=0;
56 }
57}
58
63
65{
66 // prevent against overflow
69 return table[customRandomIndex++];
70}
71
72inline float RandomTable::getRangeRandom(float start, float end)
73{
74 return (start + ((end - start) * getUnitRandom()));
75}
76
78{
79 // using our Mersenne Twister (preferred way)
80 std::mt19937 rng;
81 for(unsigned long i = 0; i < tableSize; i++)
82 table[i] = float(rng())/rng.max();
83}
84
85#endif // __RandomTable_H__
86
float getUnitRandom()
Definition RandomTable.h:64
unsigned long tableSize
Definition RandomTable.h:35
void generateRandomNumbers()
Definition RandomTable.h:77
float getRangeRandom(float start, float end)
Definition RandomTable.h:72
float * table
Definition RandomTable.h:36
void resetRandomIndex()
Definition RandomTable.h:59
unsigned long customRandomIndex
Definition RandomTable.h:37
RandomTable(unsigned long size=0x8000)
Definition RandomTable.h:44