tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
50
Task/Probabilistic-choice/Icon/probabilistic-choice.icon
Normal file
50
Task/Probabilistic-choice/Icon/probabilistic-choice.icon
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
record Item(value, probability)
|
||||
|
||||
procedure find_item (items, v)
|
||||
sum := 0.0
|
||||
every item := !items do {
|
||||
if v < sum+item.probability
|
||||
then return item.value
|
||||
else sum +:= item.probability
|
||||
}
|
||||
fail # v exceeded 1.0
|
||||
end
|
||||
|
||||
# -- helper procedures
|
||||
|
||||
# count the number of occurrences of i in list l,
|
||||
# assuming the items are strings
|
||||
procedure count (l, i)
|
||||
result := 0.0
|
||||
every x := !l do
|
||||
if x == i then result +:= 1
|
||||
return result
|
||||
end
|
||||
|
||||
procedure rand_float ()
|
||||
return ?1000/1000.0
|
||||
end
|
||||
|
||||
# -- test the procedure
|
||||
procedure main ()
|
||||
items := [
|
||||
Item("aleph", 1/5.0),
|
||||
Item("beth", 1/6.0),
|
||||
Item("gimel", 1/7.0),
|
||||
Item("daleth", 1/8.0),
|
||||
Item("he", 1/9.0),
|
||||
Item("waw", 1/10.0),
|
||||
Item("zayin", 1/11.0),
|
||||
Item("heth", 1759/27720.0)
|
||||
]
|
||||
|
||||
# collect a sample of results
|
||||
sample := []
|
||||
every (1 to 1000000) do push (sample, find_item(items, rand_float ()))
|
||||
|
||||
# return comparison of expected vs actual probability
|
||||
every item := !items do
|
||||
write (right(item.value, 7) || " " ||
|
||||
left(item.probability, 15) || " " ||
|
||||
left(count(sample, item.value)/*sample, 6))
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue