Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/prngs/prng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ double PseudoRandomNumberGenerator::generateFloatingPointRandomValue(float min,

int64_t PseudoRandomNumberGenerator::generateIntegerRandomValue(int32_t min, int32_t max) {
double random_val = generateUnitNormalRandomValue();
double random_val_magnitude = random_val * (max - min + 1);
int64_t scaled_value = static_cast<int64_t>(min + random_val_magnitude);
int64_t inclusive_range = max - min + 1;
double random_val_magnitude = random_val * static_cast<double>(inclusive_range);
int64_t scaled_value = static_cast<int64_t>(random_val_magnitude) + min;
return scaled_value;
}

Expand Down
52 changes: 51 additions & 1 deletion tests/test_LinearCongruentialGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ TEST(TestLinearCongruentialGenerator, GenerateIntegerRandomValueEquidistributed)
}


TEST(TestLinearCongruentialGenerator, GenerateIntegerRandomValueProducesInclusiveRange){
TEST(TestLinearCongruentialGenerator, GenerateIntegerRandomValueProducesInclusiveRangePositive){
LinearCongruentialGenerator lcg = LinearCongruentialGenerator();
const int32_t min = 1;
const int32_t max = 6;
Expand All @@ -146,6 +146,56 @@ TEST(TestLinearCongruentialGenerator, GenerateIntegerRandomValueProducesInclusiv
}
}

ASSERT_TRUE(found_min);
ASSERT_TRUE(found_max);
}

TEST(TestLinearCongruentialGenerator, GenerateIntegerRandomValueProducesInclusiveRangeNegative){
LinearCongruentialGenerator lcg = LinearCongruentialGenerator();
const int32_t min = -6;
const int32_t max = -1;
const int max_iterations = 1000;
bool found_min = false;
bool found_max = false;

for (int i = 0; i < max_iterations; ++i) {
int64_t value = lcg.generateIntegerRandomValue(min, max);
if (value == min) {
found_min = true;
}
if (value == max) {
found_max = true;
}
if (found_min && found_max) {
break; // No need to continue if both values are found
}
}

ASSERT_TRUE(found_min);
ASSERT_TRUE(found_max);
}

TEST(TestLinearCongruentialGenerator, GenerateIntegerRandomValueProducesInclusiveRangeNegativeAndPositive){
LinearCongruentialGenerator lcg = LinearCongruentialGenerator();
const int32_t min = -10;
const int32_t max = 10;
const int max_iterations = 1000;
bool found_min = false;
bool found_max = false;

for (int i = 0; i < max_iterations; ++i) {
int64_t value = lcg.generateIntegerRandomValue(min, max);
if (value == min) {
found_min = true;
}
if (value == max) {
found_max = true;
}
if (found_min && found_max) {
break; // No need to continue if both values are found
}
}

ASSERT_TRUE(found_min);
ASSERT_TRUE(found_max);
}
52 changes: 51 additions & 1 deletion tests/test_MersenneTwister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ TEST(TestMersenneTwister, GenerateIntegerRandomValueEquidistributed){
}


TEST(TestMersenneTwister, GenerateIntegerRandomValueProducesInclusiveRange){
TEST(TestMersenneTwister, GenerateIntegerRandomValueProducesInclusiveRangePositive){
MersenneTwister mt = MersenneTwister();
const int32_t min = 1;
const int32_t max = 6;
Expand All @@ -132,6 +132,56 @@ TEST(TestMersenneTwister, GenerateIntegerRandomValueProducesInclusiveRange){
}
}

ASSERT_TRUE(found_min);
ASSERT_TRUE(found_max);
}

TEST(TestMersenneTwister, GenerateIntegerRandomValueProducesInclusiveRangeNegative){
MersenneTwister mt = MersenneTwister();
const int32_t min = -6;
const int32_t max = -1;
const int max_iterations = 1000;
bool found_min = false;
bool found_max = false;

for (int i = 0; i < max_iterations; ++i) {
int64_t value = mt.generateIntegerRandomValue(min, max);
if (value == min) {
found_min = true;
}
if (value == max) {
found_max = true;
}
if (found_min && found_max) {
break; // No need to continue if both values are found
}
}

ASSERT_TRUE(found_min);
ASSERT_TRUE(found_max);
}

TEST(TestMersenneTwister, GenerateIntegerRandomValueProducesInclusiveRangeNegativeAndPositive){
MersenneTwister mt = MersenneTwister();
const int32_t min = -10;
const int32_t max = 10;
const int max_iterations = 1000;
bool found_min = false;
bool found_max = false;

for (int i = 0; i < max_iterations; ++i) {
int64_t value = mt.generateIntegerRandomValue(min, max);
if (value == min) {
found_min = true;
}
if (value == max) {
found_max = true;
}
if (found_min && found_max) {
break; // No need to continue if both values are found
}
}

ASSERT_TRUE(found_min);
ASSERT_TRUE(found_max);
}
56 changes: 54 additions & 2 deletions tests/test_XORShift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TEST(TestXORShift, GenerateIntegerRandomValueEquidistributed){
ASSERT_NEAR(expected_value, average, margin_of_error);
}

TEST(TestXORShift, GenerateIntegerRandomValueProducesInclusiveRange){
TEST(TestXORShift, GenerateIntegerRandomValueProducesInclusiveRangePositive){
XORShift xor_shift = XORShift();
const int32_t min = 1;
const int32_t max = 6;
Expand All @@ -146,4 +146,56 @@ TEST(TestXORShift, GenerateIntegerRandomValueProducesInclusiveRange){

ASSERT_TRUE(found_min);
ASSERT_TRUE(found_max);
}
}

TEST(TestXORShift, GenerateIntegerRandomValueProducesInclusiveRangeNegative){
XORShift xor_shift = XORShift();
const int32_t min = -6;
const int32_t max = -1;
const int max_iterations = 1000;
bool found_min = false;
bool found_max = false;

for (int i = 0; i < max_iterations; ++i) {
int64_t value = xor_shift.generateIntegerRandomValue(min, max);
if (value == min) {
found_min = true;
}
if (value == max) {
found_max = true;
}
if (found_min && found_max) {
break; // No need to continue if both values are found
}
}

ASSERT_TRUE(found_min);
ASSERT_TRUE(found_max);
}

TEST(TestXORShift, GenerateIntegerRandomValueProducesInclusiveRangeNegativeAndPositive){
XORShift xor_shift = XORShift();
const int32_t min = -10;
const int32_t max = 10;
const int max_iterations = 1000;
bool found_min = false;
bool found_max = false;

for (int i = 0; i < max_iterations; ++i) {
int64_t value = xor_shift.generateIntegerRandomValue(min, max);
if (value == min) {
found_min = true;
}
if (value == max) {
found_max = true;
}
if (found_min && found_max) {
break; // No need to continue if both values are found
}
}

ASSERT_TRUE(found_min);
ASSERT_TRUE(found_max);
}