Re: ISO help in probability
- From: Uwe Klein <uwe_klein_habertwedt@xxxxxxxxxxx>
- Date: Fri, 09 Dec 2005 14:31:56 +0100
Helmut Giese wrote:
Hello out there, I have a set of variables each of which has a certain weight or probability assigned. Say a -> 50 b -> 30 c -> 10 d -> 10
Hi Helmut, This any better?:
#!/usr/bin/tclsh
set probabilities {a 50 b 30 c 10 d 10 }set curr 0
foreach {tok prob} $probabilities {
lappend ::stat(toks) $tok
set ::stat($tok,min) $curr
incr curr $prob
set ::stat($tok,max) $currset ::res($tok) 0 } set ::stat(summ) $curr
# rand() gives an even distribution from 0.0 to 0.999999999
proc randtok {} {
set rand [ expr { rand() * $::stat(summ) }]
foreach tok $::stat(toks) {
if { $rand >= $::stat($tok,min) } {
if { $rand < $::stat($tok,max) } {
return $tok
}
}
}
return this_is_an_error
}for {set i 0} {$i < 10000 } {incr i} {
incr ::res([randtok])
}
parray ::res#end
G! uwe .
- References:
- ISO help in probability
- From: Helmut Giese
- ISO help in probability
- Prev by Date: Re: status bar with two labels
- Next by Date: Re: problem with status bar
- Previous by thread: Re: ISO help in probability
- Next by thread: Re: ISO help in probability
- Index(es):
Relevant Pages
|