From 8867dcfa99a112c4d40ae22acdfbc7c68615ab65 Mon Sep 17 00:00:00 2001 From: jenni-westoby Date: Wed, 18 Oct 2017 20:17:25 +0100 Subject: [PATCH] Fix assertion error for arrays with lots of zeros. The logic for sampling arrays fails when most of the array is zero. We fix this by sampling uniformly from the indices into the array, as suggested by the comment in simul.h. --- simul.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/simul.h b/simul.h index b62132a..5a2201b 100644 --- a/simul.h +++ b/simul.h @@ -18,6 +18,10 @@ class simul { int l, r, mid; double prb = random() * arr[len - 1]; + // if the final value is zero, pick a random index from the array. + if (arr[len - 1] == 0.0) { + return random() * (len - 1); + } l = 0; r = len - 1; while (l <= r) { @@ -40,4 +44,3 @@ class simul { }; #endif /* SIMUL_H_ */ -