
Task

Write a computer program that computes and shows its own   entropy.



Related Tasks

   Fibonacci_word
   Entropy


' version 01-06-2016
' compile with: fbc -s console
' modified code from ENTROPY entry
 
Dim As Integer i, count, totalchar(255)
Dim As UByte buffer
Dim As Double prop, entropy
' command (0) returns the name of this program (including the path)
Dim As String slash, filename = Command(0)
Dim As Integer ff = FreeFile   ' find first free filenumber
Open filename For Binary As #ff
 
If Err > 0 Then ' should not happen
    Print "Error opening the file"
    Beep : Sleep 5000, 1
    End
End If
 
' will read 1 UByte from the file until it reaches the end of the file
For i = 1 To Lof(ff)
    Get #ff, ,buffer
    totalchar(buffer) += 1
    count = count + 1
Next
 
For i = 0  To 255
    If totalchar(i) = 0 Then Continue For
    prop = totalchar(i) / count
    entropy = entropy - (prop * Log (prop) / Log(2))
Next
 
' next lines are only compiled when compiling for Windows OS (32/64)
#Ifdef __FB_WIN32__
    slash = chr(92)
    print "Windows version"
#endif    
#Ifdef __FB_LINUX__
   slash = chr(47)
   print "LINUX version"
#EndIf
 
    i = InStrRev(filename, slash)
    If i <> 0 Then filename = Right(filename, Len(filename)-i)
 
Print "My name is "; filename
Print : Print "The Entropy of myself is"; entropy
Print
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End