From 42fc243d720d95a11d38ab9cd4ae2eb34d37b3b2 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 7 Sep 2021 15:06:21 -0400 Subject: =?UTF-8?q?Add=20=E2=80=A2rand=20with=20Range=20and=20Deal=20funct?= =?UTF-8?q?ions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/bqn.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'docs') diff --git a/docs/bqn.js b/docs/bqn.js index e1994530..88492d59 100644 --- a/docs/bqn.js +++ b/docs/bqn.js @@ -563,10 +563,35 @@ let mathns = obj2ns(Math, ); trig.map((_,i)=>{let f=mathns[i],g=mathns[i+trig.length]; f.inverse=g; g.inverse=f;}); +let rand = (() => { + let reqnat = (e,x) => { + if (!isnum(x) || x<0 || x!=Math.floor(x)) throw Error("•rand."+e+" must be a natural number"); + }; + let randnat = n => Math.floor(n*Math.random()); + let range = (x,w) => { + reqnat("Range: 𝕩", x); + let r = x ? (()=>randnat(x)) : Math.random; + return has(w) ? list(Array(w).fill().map(r)) : r(); + }; + let deal = (x,w) => { + reqnat("Deal: 𝕩", x); + if (!has(w)) w=x; else { + reqnat("Deal: 𝕨", w); if (w>x) throw Error("•rand.Deal: 𝕨 must be less than or equal to 𝕩"); + } + let r = Array(x).fill().map((_,i)=>i); + for (let i=0; irun(...r))), rebqn, primitives, type, glyph, decompose, fmt:fmt1, repr, unixtime, listkeys, - js:dojs, math:mathns, + js:dojs, math:mathns, rand, listsys: dynsys(_ => list(Object.keys(sysvals).sort().map(str))) }; -- cgit v1.2.3