RosettaCodeData/Task/Entropy/Run-BASIC/entropy.basic

27 lines
622 B
Plaintext

dim chrCnt( 255) ' possible ASCII chars
source$ = "1223334444"
numChar = len(source$)
for i = 1 to len(source$) ' count which chars are used in source
ch$ = mid$(source$,i,1)
if not( instr(chrUsed$, ch$)) then chrUsed$ = chrUsed$ + ch$
j = instr(chrUsed$, ch$)
chrCnt(j) =chrCnt(j) +1
next i
lc = len(chrUsed$)
for i = 1 to lc
odds = chrCnt(i) /numChar
entropy = entropy - (odds * (log(odds) / log(2)))
next i
print " Characters used and times used of each "
for i = 1 to lc
print " '"; mid$(chrUsed$,i,1); "'";chr$(9);chrCnt(i)
next i
print " Entropy of '"; source$; "' is "; entropy; " bits."
end