here is a working weighted random choice code
Alex i borrowed a few lines from yours and adapted it to made it work for the weights.
output shows the random order and the number of times each item was chosen.
lines 23-35 are there just to show the weighted count is correct. You can remove them
Function WeightedRandomChoice
Alex i borrowed a few lines from yours and adapted it to made it work for the weights.
output shows the random order and the number of times each item was chosen.
lines 23-35 are there just to show the weighted count is correct. You can remove them
Function WeightedRandomChoice
int apples pears bananas oranges
str ss=
;apple 9
;pear 1
;banana 10
;orange 20
str s s1
int i
foreach s ss
,s.trim
,if findrx(s "\d+$" 0 2 s1)<0
,,end F"line: '{s}' needs a number"
,s.findreplace(s1)
,ARRAY(str) a
,for i 0 val(s1)
,,a[]=s
for i a.len-1 -1 -1
,a.shuffle
,_s=a[i]
,_s.trim
,out _s
,a.remove(i)
,sel _s
,,case "apple"
,,apples+1
,,case "pear"
,,pears+1
,,case "banana"
,,bananas+1
,,case "orange"
,,oranges+1
out apples
out pears
out bananas
out oranges