numbas / Numbas

A completely browser-based e-assessment/e-learning system, with an emphasis on mathematics
http://www.numbas.org.uk
Apache License 2.0
200 stars 118 forks source link

added weighted_random function with signature (list of [item, probability]) to handle issue #839 #843

Closed kalpitf1 closed 3 years ago

kalpitf1 commented 3 years ago

Addresses issue #839 Added weighted_random function to runtime/scripts/math.js with the function signature (list of [item, probability]) and defined a JME function in runtime/scripts/jme-builtins.js with input list of [TNum, TNum] and output of TNum. The weighted_random function can handle item of type int, float, string. Please let me know how we can add those to the JME function definition. I tested the weighted_random function logic externally but did not know how to write unit tests. Would love to learn how to do so. Hope this helps!