diff --git a/arrays.scad b/arrays.scad index e2a452c..28f4607 100644 --- a/arrays.scad +++ b/arrays.scad @@ -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]) diff --git a/math.scad b/math.scad index 47dc3d9..aada20f 100644 --- a/math.scad +++ b/math.scad @@ -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)