Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weighted Random Choice
#5
Hello!
Please, try this:

Macro Use WeightedRandomChoices
Code:
Copy      Help
lpstr T=
;apple 9
;pear 1
;banana 10
;orange 20

out
out WeightedRandomChoices(T)

Function WeightedRandomChoices
Code:
Copy      Help
;/
function'str lpstr'T

str s s1
int i
ARRAY(str) a

foreach s T
,s.trim
,if findrx(s "\d+$" 0 2 s1)<0
,,end F"line: '{s}' needs a number"
,s.findreplace(s1)
,for i 0 val(s1)
,,a[]=s

a.shuffle

ret a[0]

Regards Smile


Messages In This Thread
Weighted Random Choice - by sirpipthegreat - 09-04-2018, 01:28 AM
RE: Weighted Random Choice - by Start_Learning - 09-05-2018, 07:50 PM
RE: Weighted Random Choice - by sirpipthegreat - 09-05-2018, 09:05 PM
RE: Weighted Random Choice - by Kevin - 09-06-2018, 02:26 AM
RE: Weighted Random Choice - by AlexZ - 09-06-2018, 03:25 AM
RE: Weighted Random Choice - by Kevin - 09-06-2018, 06:42 AM
RE: Weighted Random Choice - by sirpipthegreat - 09-06-2018, 01:02 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)