Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weighted Random Choice
#2
I'm not sure what you really want, but try this to see if it fits:
Macro Weighted Random Choice
Code:
Copy      Help
;dictionary fruit.txt would have several arrays similar to this:
;{
;"apple", 9
;"pear", 1
;"banana", 10
;"orange", 20
;}

;Suppose the file dictionary fruit.txt is in the "Desktop" folder.
;You could change the directory to where the file is actually belongs to.
str s1.getfile("$desktop$\fruit.txt")

;Create an IStringMap variable to hold the result and weight values.
IStringMap m._create
lpstr fruit_mapping=sub.createDictMapping(s1)

;Assigned fruit list to IStringMap
m.AddList(fruit_mapping)

;Retrieve the result
out
ARRAY(str) allKeys, allValues
m.GetAll(allKeys, allValues)
int i, sum
for i 0 allValues.len
,sum +=val(allValues[i])

for i 0 allKeys.len
,_s = allKeys[i]
,out F"{_s} = {m.Get(_s)}/{sum}"


#sub createDictMapping
function~ $dict

str line
foreach line dict
,if(line = "{") continue
,if(line = "}") continue
,line.findreplace("''" "" 8)
,line.findreplace("," "" 8)
,_s.addline(line)

ret _s


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: 7 Guest(s)