1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-01-29 11:58:39 +01:00

add choose

This commit is contained in:
Justin Lin 2021-01-24 11:33:52 +08:00
parent 49b3425bc1
commit 0d246f1a22
2 changed files with 29 additions and 0 deletions

16
docs/lib2x-choose.md Normal file
View File

@ -0,0 +1,16 @@
# choose
Choose an element from the given list randomly.
**Since:** 2.5
## Parameters
- `choices` : The list to choose from.
- `seed` : Random seed value.
## Examples
use <util/choose.scad>;
echo(choose([1, 2, 3, 4]));

13
src/util/choose.scad Normal file
View File

@ -0,0 +1,13 @@
/**
* choose.scad
*
* @copyright Justin Lin, 2021
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib2x-choose.html
*
**/
function choose(choices, seed) =
let(c = is_undef(seed) ? rands(0, len(choices) - 1, 1) : rands(0, len(choices) - 1, 1, seed))
choices[round(c[0])];