RosettaCodeData/Task/Statistics-Basic/FreeBASIC/statistics-basic.basic

70 lines
1.6 KiB
Plaintext

' FB 1.05.0 Win64
Randomize
Sub basicStats(sampleSize As Integer)
If sampleSize < 1 Then Return
Dim r(1 To sampleSize) As Double
Dim h(0 To 9) As Integer '' all zero by default
Dim sum As Double = 0.0
Dim hSum As Integer = 0
' Generate 'sampleSize' random numbers in the interval [0, 1)
' calculate their sum
' and in which box they will fall when drawing the histogram
For i As Integer = 1 To sampleSize
r(i) = Rnd
sum += r(i)
h(Int(r(i) * 10)) += 1
Next
For i As Integer = 0 To 9 : hSum += h(i) : Next
' adjust one of the h() values if necessary to ensure hSum = sampleSize
Dim adj As Integer = sampleSize - hSum
If adj <> 0 Then
For i As Integer = 0 To 9
h(i) += adj
If h(i) >= 0 Then Exit For
h(i) -= adj
Next
End If
Dim mean As Double = sum / sampleSize
Dim sd As Double
sum = 0.0
' Now calculate their standard deviation
For i As Integer = 1 To sampleSize
sum += (r(i) - mean) ^ 2.0
Next
sd = Sqr(sum/sampleSize)
' Draw a histogram of the data with interval 0.1
Dim numStars As Integer
' If sample size > 500 then normalize histogram to 500
Dim scale As Double = 1.0
If sampleSize > 500 Then scale = 500.0 / sampleSize
Print "Sample size "; sampleSize
Print
Print Using " Mean #.######"; mean;
Print Using " SD #.######"; sd
Print
For i As Integer = 0 To 9
Print Using " #.## : "; i/10.0;
Print Using "##### " ; h(i);
numStars = Int(h(i) * scale + 0.5)
Print String(numStars, "*")
Next
End Sub
basicStats 100
Print
basicStats 1000
Print
basicStats 10000
Print
basicStats 100000
Print
Print "Press any key to quit"
Sleep