aboutsummaryrefslogtreecommitdiff
path: root/builtins/array.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-03 17:44:22 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-03 17:44:22 -0400
commit09204f4ce85f49f0b1108223dc271b4405a3c663 (patch)
tree4d3e9087f7bedb61a86f7c9bbed162be2028021c /builtins/array.c
parent8357d0299207ddb18772915378e396e92184b0fe (diff)
Revert "Fix array:random() to return a random item using correct RNG logic,"
This reverts commit 68b34cf00b8a52509c0bed7b1e66b3e40de0c4f5.
Diffstat (limited to 'builtins/array.c')
-rw-r--r--builtins/array.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/builtins/array.c b/builtins/array.c
index 9b0e4206..3b1b7c6d 100644
--- a/builtins/array.c
+++ b/builtins/array.c
@@ -13,7 +13,6 @@
#include "array.h"
#include "functions.h"
#include "halfsiphash.h"
-#include "integers.h"
#include "types.h"
#include "util.h"
@@ -161,7 +160,7 @@ public void Array$shuffle(array_t *arr, int64_t padded_item_size)
char tmp[padded_item_size];
for (int64_t i = arr->length-1; i > 1; i--) {
- int64_t j = Int$random(0, i);
+ int32_t j = arc4random_uniform(i+1);
memcpy(tmp, arr->data + i*padded_item_size, padded_item_size);
memcpy((void*)arr->data + i*padded_item_size, arr->data + j*padded_item_size, padded_item_size);
memcpy((void*)arr->data + j*padded_item_size, tmp, padded_item_size);
@@ -172,7 +171,7 @@ public void *Array$random(array_t arr)
{
if (arr.length == 0)
return NULL; // fail("Cannot get a random item from an empty array!");
- int64_t index = Int$random(0, arr.length-1);
+ uint32_t index = arc4random_uniform(arr.length);
return arr.data + arr.stride*index;
}
@@ -204,7 +203,7 @@ public array_t Array$sample(array_t arr, int64_t n, array_t weights, int64_t pad
if (total == 0.0) {
for (int64_t i = 0; i < n; i++) {
- int64_t index = Int$random(0, arr.length-1);
+ uint32_t index = arc4random_uniform(arr.length);
memcpy(selected.data + i*padded_item_size, arr.data + arr.stride*index, padded_item_size);
}
} else {