RosettaCodeData/Task/Probabilistic-choice/FreeBASIC/probabilistic-choice.freebasic

60 lines
1.4 KiB
Plaintext

' FB 1.05.0 Win64
Dim letters (0 To 7) As String = {"aleph", "beth", "gimel", "daleth", "he", "waw", "zayin", "heth"}
Dim actual (0 To 7) As Integer '' all zero by default
Dim probs (0 To 7) As Double = {1/5.0, 1/6.0, 1/7.0, 1/8.0, 1/9.0, 1/10.0, 1/11.0}
Dim cumProbs (0 To 7) As Double
cumProbs(0) = probs(0)
For i As Integer = 1 To 6
cumProbs(i) = cumProbs(i - 1) + probs(i)
Next
cumProbs(7) = 1.0
probs(7) = 1.0 - cumProbs(6)
Randomize
Dim rand As Double
Dim n As Double = 1000000
Dim sum As Double = 0.0
For i As Integer = 1 To n
rand = Rnd '' random number where 0 <= rand < 1
Select case rand
Case Is <= cumProbs(0)
actual(0) += 1
Case Is <= cumProbs(1)
actual(1) += 1
Case Is <= cumProbs(2)
actual(2) += 1
Case Is <= cumProbs(3)
actual(3) += 1
Case Is <= cumProbs(4)
actual(4) += 1
Case Is <= cumProbs(5)
actual(5) += 1
Case Is <= cumProbs(6)
actual(6) += 1
Case Else
actual(7) += 1
End Select
Next
Dim sumActual As Double = 0
Print "Letter", " Actual", "Expected"
Print "------", "--------", "--------"
For i As Integer = 0 To 7
Print letters(i),
Print Using "#.######"; actual(i)/n;
sumActual += actual(i)/n
Print , Using "#.######"; probs(i)
Next
Print , "--------", "--------"
Print , Using "#.######"; sumActual;
Print , Using "#.######"; 1.000000
Print
Print "Press any key to quit"
Sleep