Skip to content

Commit 5dad23b

Browse files
committed
do not allow random ranges to return negative values
1 parent dafe436 commit 5dad23b

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

src/my_random.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
//
1717
#define pcg_rand() pcg32_random()
1818
#define pcg_srand(a) pcg32_srandom(a, a)
19-
#define pcg_random_range(a, b) (pcg32_boundedrand((b) - (a)) + (a))
20-
#define pcg_random_range_inclusive(a, b) (pcg32_boundedrand((b) - (a) + 1) + (a))
19+
#define pcg_random_range(a, b) ((b) > (a) ? pcg32_boundedrand((b) - (a)) + (a) : (a))
20+
#define pcg_random_range_inclusive(a, b) ((b) > (a) ? pcg32_boundedrand((b) - (a) + 1) + (a) : (a))
2121

2222
//
2323
// For randomness that does not matter for reproducability.
2424
//
2525
#define non_pcg_rand() rand()
2626
#define non_pcg_srand(a) srand(a)
27-
#define non_pcg_random_range(a, b) (rand() % ((b) - (a)) + (a))
28-
#define non_pcg_random_range_inclusive(a, b) (rand() % ((b) - (a) + 1) + (a))
27+
#define non_pcg_random_range(a, b) ((b) > (a) ? rand() % ((b) - (a)) + (a) : (a))
28+
#define non_pcg_random_range_inclusive(a, b) ((b) > (a) ? rand() % ((b) - (a) + 1) + (a) : (a))
2929

3030
#endif

0 commit comments

Comments
 (0)