Skip to content

Commit c90cfc2

Browse files
rscharfegitster
authored andcommitted
test-mergesort: use repeatable random numbers
Use MINSTD to generate pseudo-random numbers consistently instead of using rand(3), whose output can vary from system to system, and reset its seed before filling in the test values. This gives repeatable results across versions and systems, which simplifies sharing and comparing of results between developers. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent afc72b5 commit c90cfc2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

t/helper/test-mergesort.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
#include "cache.h"
33
#include "mergesort.h"
44

5+
static uint32_t minstd_rand(uint32_t *state)
6+
{
7+
*state = (uint64_t)*state * 48271 % 2147483647;
8+
return *state;
9+
}
10+
511
struct line {
612
char *text;
713
struct line *next;
@@ -60,8 +66,9 @@ static void dist_sawtooth(int *arr, int n, int m)
6066
static void dist_rand(int *arr, int n, int m)
6167
{
6268
int i;
69+
uint32_t seed = 1;
6370
for (i = 0; i < n; i++)
64-
arr[i] = rand() % m;
71+
arr[i] = minstd_rand(&seed) % m;
6572
}
6673

6774
static void dist_stagger(int *arr, int n, int m)
@@ -81,8 +88,9 @@ static void dist_plateau(int *arr, int n, int m)
8188
static void dist_shuffle(int *arr, int n, int m)
8289
{
8390
int i, j, k;
91+
uint32_t seed = 1;
8492
for (i = j = 0, k = 1; i < n; i++)
85-
arr[i] = (rand() % m) ? (j += 2) : (k += 2);
93+
arr[i] = minstd_rand(&seed) % m ? (j += 2) : (k += 2);
8694
}
8795

8896
#define DIST(name) { #name, dist_##name }

0 commit comments

Comments
 (0)