Shafayat Posted September 4, 2009 Posted September 4, 2009 Code to randomize based on different probability. $rec = RndwithProbablity('a=5,b=2,c=3') MsgBox(0,"",$rec) Func RndwithProbablity($valarray) Local $name[100], $val[100], $temp, $temp2, $total, $ptot, $ntot, $rnd $temp = StringSplit($valarray,",") $total = 0 For $i = 1 To $temp[0] $temp2 = StringSplit($temp[$i],"=") $name[$i] = $temp2[1] $val[$i] = $temp2[2] $total = $total + $temp2[2] Next $rnd = Random(1, $total, 1) For $i = 1 To $temp[0] $ntot = 0 For $j = 1 To $i $ntot = $ntot + $val[$j] Next $ptot = 0 If $i = 1 Then $ptot = 0 Else For $j = 1 To ($i-1) $ptot = $ptot + $val[$j] Next EndIf If ($rnd > $ptot And $rnd <= $ntot) Then Return $name[$i] ExitLoop EndIf Next EndFunc [Not using this account any more. Using "iShafayet" instead]
Shafayat Posted September 4, 2009 Author Posted September 4, 2009 Application of Mendels 2nd law (using this function) expandcollapse popupFor $test = 1 To 2000 Dim $a, $b, $c, $d $rec = RndwithProbablity('a=9,b=3,c=3,d=1') If $rec = 'a' Then $a = $a + 1 If $rec = 'b' Then $b = $b + 1 If $rec = 'c' Then $c = $c + 1 If $rec = 'd' Then $d = $d + 1 Next MsgBox(0,"Application of Mendels 2nd law","A Tall & Yellow (male) and a Short & White (female) gave birth to 2000 siblings. Amongst them: " & @CRLF & @CRLF & "Tall & Yellow = " & $a & @CRLF & "Tall & White = " & $b & @CRLF & "Short & Yellow = " & $c & @CRLF & "Short & White = " & $d & @CRLF _ & @CRLF & "The ration is " & Int($a/$d) & ":" & Int($b/$d) & ":" & Int($c/$d) & ":" & $d/$d & " (" & Int($a/$d*100)/100 & ":" & Int($b/$d*100)/100 & ":" & Int($c/$d*100)/100 & ":" & $d/$d & ")" & @CRLF & "The ideal ration is 9:3:3:1") Func RndwithProbablity($valarray) Local $name[100], $val[100], $temp, $temp2, $total, $ptot, $ntot, $rnd $temp = StringSplit($valarray,",") $total = 0 For $i = 1 To $temp[0] $temp2 = StringSplit($temp[$i],"=") $name[$i] = $temp2[1] $val[$i] = $temp2[2] $total = $total + $temp2[2] Next $rnd = Random(1, $total, 1) For $i = 1 To $temp[0] $ntot = 0 For $j = 1 To $i $ntot = $ntot + $val[$j] Next $ptot = 0 If $i = 1 Then $ptot = 0 Else For $j = 1 To ($i-1) $ptot = $ptot + $val[$j] Next EndIf If ($rnd > $ptot And $rnd <= $ntot) Then Return $name[$i] ExitLoop EndIf Next EndFunc [Not using this account any more. Using "iShafayet" instead]
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now