Added rand_ints() and shuffle()

This commit is contained in:
Revar Desmera 2019-05-29 17:42:09 -07:00
parent 34f9677035
commit 62f913a148
2 changed files with 28 additions and 0 deletions

View file

@ -244,6 +244,18 @@ function enumerate(l,idx=undef) =
[for (i=[0:1:len(l)-1]) concat([i], [for (j=idx) l[i][j]])];
// Function: shuffle(list)
// Description:
// Shuffles the input list into random order.
function shuffle(list) =
len(list)<=1 ? list :
let (
rval = rands(0,1,len(list)),
left = [for (i=[0:len(list)-1]) if (rval[i]< 0.5) list[i]],
right = [for (i=[0:len(list)-1]) if (rval[i]>=0.5) list[i]]
) concat(shuffle(left), shuffle(right));
// Function: sort()
// Usage:
// sort(arr, [idx])

View file

@ -143,6 +143,22 @@ function modrange(x, y, m, step=1) =
function sqr(x) = x*x;
// Function: rand_int(min,max,N,seed)
// Usage:
// rand_int(min,max,N,[seed]);
// Description:
// Return a list of random integers in the range of min to max, inclusive.
// Arguments:
// min = Minimum integer value to return.
// max = Maximum integer value to return.
// N = Number of random integers to return.
// seed = Random number seed.
function rand_int(min,max,N,seed=undef) =
assert(max >= min, "Max value cannot be smaller than min")
let (rvect = is_def(seed) ? rands(min,max+1,N,seed) : rands(min,max+1,N))
[for(entry = rvect) floor(entry)];
// Function: gaussian_rand()
// Usage:
// gaussian_rand(mean, stddev)