Thanks [VxE] for the original code!
USAGE + FUNCTION (download)
;======================================================================== ; ; Function: WRandom ; Description: Gets a random field from a weighted set ; Online Ref.: http://www.autohotkey.com/forum/viewtopic.php?t=47586 ; ; Last Update: 12/August/2009 19:30 ; ; Created by: MasterFocus ; https://ahknet.autohotkey.com/~MasterFocus/ ; ; Thanks to: [VxE] for original code/algorithm ; www.autohotkey.com/forum/viewtopic.php?p=287940#287940 ; ;======================================================================== ; ; p_FieldString [, p_Chance, p_P2D, p_D2P] ; ; + Required parameters: ; - p_FieldString CSV string containing a decimal weight for each field ; ; + Optional parameters: ; - p_Chance Boolean, shows chance (%) the selected field had ; - p_P2D Boolean, shows 1% in decimal ; - p_D2P Boolean, shows 1 (decimal) in percentage ; ; The function returns the number of the selected field. ; If any optional parameters are present, they will be concatenated to ; the returned string (CSV) in the order they are presented. ; ;======================================================================== WRandom(p_FieldString,p_Chance=false,p_P2D=false,p_D2P=false) { Loop, Parse, p_FieldString, `, l_Tot += A_LoopField Random, l_Sum, 0, % l_Tot Loop, Parse, p_FieldString, `, If ( 0 >= l_Sum -= A_LoopField ) Return A_Index ( p_Chance ? "," (100*A_LoopField)/l_Tot : "" ) ( p_P2D ? "," l_Tot/100 : "" ) ( p_D2P ? "," 100/l_Tot : "" ) }
EXAMPLE (download)
#Include WRandom.ahk ; ============================= MsgBox % "** EXAMPLE 1 **" aux := "Field , Chance , %->Dec , Dec->%`n" aux .= "`n" WRandom("50,50",1,1,1) aux .= "`n" WRandom("20,30",1,1,1) aux .= "`n" WRandom("30,30,40",1,1,1) aux .= "`n" WRandom("200,600",1,1,1) aux .= "`n" WRandom("99,1",1,1,1) aux .= "`n" WRandom("15,14",1,1,1) aux .= "`n" WRandom("123,276,57,93",1,1,1) aux .= "`n" WRandom("50.3,27.9,36.5,42.8,50.3,27.9,36.5,42.8,50.3,27.9,6.5,42.8",1,1,1) MsgBox % aux ; ============================= MsgBox % "** EXAMPLE 2 **" aux := "" Loop, 9 aux .= ( aux ? "," : "" ) Field%A_Index% := (A_Index*10)+A_Index aux := WRandom(aux,1) ; same as WRandom( "11,22,33,44,55,66,77,88,99" , 1 ) Loop, Parse, aux, `, If ( A_Index = 1 ) num := A_LoopField Else aux := A_LoopField MsgBox % "Field " num "/9 was selected (had " aux "% chance!)" ; ============================= Return