Skip to content

Commit

Permalink
cross platform random numbers for samples
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Aug 31, 2024
1 parent e6cbbfc commit 1b57543
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
6 changes: 5 additions & 1 deletion samples/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ Sample::Sample( Settings& settings )
m_maxProfile = {};
m_totalProfile = {};

g_seed = RAND_SEED;

TestMathCpp();
}

Expand Down Expand Up @@ -488,10 +490,12 @@ int RegisterSample( const char* category, const char* name, SampleCreateFcn* fcn
return -1;
}

uint32_t g_seed = RAND_SEED;

b2Polygon RandomPolygon( float extent )
{
b2Vec2 points[b2_maxPolygonVertices];
int count = 3 + rand() % 6;
int count = 3 + RandomInt() % 6;
for ( int i = 0; i < count; ++i )
{
points[i] = RandomVec2( -extent, extent );
Expand Down
33 changes: 24 additions & 9 deletions samples/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
// todo this include is slow
#include "TaskScheduler.h"

#include <stdlib.h>

#define ARRAY_COUNT( A ) (int)( sizeof( A ) / sizeof( A[0] ) )

struct Settings;
Expand Down Expand Up @@ -113,32 +111,49 @@ extern SampleEntry g_sampleEntries[MAX_SAMPLES];
extern int g_sampleCount;

#define RAND_LIMIT 32767
#define RAND_SEED 12345

extern uint32_t g_seed;

// Generate the next random number
inline int RandomInt()
{
// XorShift32 algorithm
uint32_t x = g_seed;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
g_seed = x;

// Map the 32-bit value to the range 0 to RAND_LIMIT
return (int)( x % ( RAND_LIMIT + 1 ) );
}

/// Random integer in range [lo, hi)
// Random integer in range [lo, hi]
inline float RandomInt( int lo, int hi )
{
return lo + rand() % ( hi - lo );
return lo + RandomInt() % ( hi - lo + 1 );
}

/// Random number in range [-1,1]
// Random number in range [-1,1]
inline float RandomFloat()
{
float r = (float)( rand() & ( RAND_LIMIT ) );
float r = (float)( RandomInt() & ( RAND_LIMIT ) );
r /= RAND_LIMIT;
r = 2.0f * r - 1.0f;
return r;
}

/// Random floating point number in range [lo, hi]
// Random floating point number in range [lo, hi]
inline float RandomFloat( float lo, float hi )
{
float r = (float)( rand() & ( RAND_LIMIT ) );
float r = (float)( RandomInt() & ( RAND_LIMIT ) );
r /= RAND_LIMIT;
r = ( hi - lo ) * r + lo;
return r;
}

/// Random vector with coordinates in range [lo, hi]
// Random vector with coordinates in range [lo, hi]
inline b2Vec2 RandomVec2( float lo, float hi )
{
b2Vec2 v;
Expand Down
2 changes: 1 addition & 1 deletion samples/sample_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BenchmarkBarrel : public Sample

void CreateScene()
{
srand( 42 );
g_seed = 42;

for ( int i = 0; i < e_maxRows * e_maxColumns; ++i )
{
Expand Down

0 comments on commit 1b57543

Please sign in to comment.