mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-04 03:09:45 +00:00
Added rand_ints() and shuffle()
This commit is contained in:
parent
34f9677035
commit
62f913a148
2 changed files with 28 additions and 0 deletions
12
arrays.scad
12
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]])];
|
[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()
|
// Function: sort()
|
||||||
// Usage:
|
// Usage:
|
||||||
// sort(arr, [idx])
|
// sort(arr, [idx])
|
||||||
|
|
16
math.scad
16
math.scad
|
@ -143,6 +143,22 @@ function modrange(x, y, m, step=1) =
|
||||||
function sqr(x) = x*x;
|
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()
|
// Function: gaussian_rand()
|
||||||
// Usage:
|
// Usage:
|
||||||
// gaussian_rand(mean, stddev)
|
// gaussian_rand(mean, stddev)
|
||||||
|
|
Loading…
Reference in a new issue