' ***********************************************************
' PROGRAM:      prob.bac
' PURPOSE:      test of randomness
' AUTHOR:               vovchik (Puppy Linux forum)
' COMMENTS:     GPL                    
' DEPENDS:      bash, bacon (b. 1.10)
' PLATFORM:     Linux (actually, any *nix)
' DATE:         16-03-2010
' VERSION:      0.1a
' ***********************************************************

OPTION BASE 1

' *****************
' CONSTANTS
' *****************

CONST Index = 200000

' *****************
' END CONSTANTS
' *****************

' *****************
' DECLARATIONS
' *****************

GLOBAL Percent, Hit, Miss, Mean, MyIndex, MySum, Square, SD TYPE FLOATING
GLOBAL Prob TYPE NUMBER
GLOBAL MyNums[Index] TYPE FLOATING

' *****************
' END DECLARATIONS
' *****************


' *****************
' FUNCTIONS
' *****************

'-----------
DEF FN PR(P) = ISTRUE(P > RANDOM(100))
'-----------
' This function returns true at the specified probability, otherwise false
' i.e. PR(70) is true 70% of the time

' *****************
' END FUNCTIONS
' *****************


' *****************
' SUBROUTINES
' *****************

'-----------
SUB INIT_VARS
'-----------
    SEED NOW
    Hit = 0.0
    Miss = 0.0
    Mean = 0.0
    MySum = 0.0
    MyIndex = Index
    MyLine$ = "-----------------------------"
END SUB

'-----------
SUB GET_INPUT
'-----------
    SPLIT ARGUMENT$ BY " " TO CliArgs$ SIZE MaxArgs
    IF LEN(CliArgs$[2]) < 1 OR VAL(CliArgs$[2]) < 0 OR VAL(CliArgs$[2]) > 99 THEN
        PRINT NL$ "This program requires an integer from 1 to 99 as the sole argument."
        PRINT NL$, "Example: prob 90", NL$
        END
    ELSE
        Prob = VAL(CliArgs$[2])
    END IF
END SUB

'-----------
SUB POPULATE_ARRAY
'-----------
    LOCAL i
    FOR i = 1 TO Index
        MyNums[i] = PR(Prob)
        IF MyNums[i] THEN
            Hit = Hit + 1
        ELSE
            Miss = Miss + -1
        END IF
    NEXT i
END SUB

'-----------
SUB CALCULATE_MEAN
'-----------
    LOCAL i
    FOR i = 1 TO Index
        MySum = MySum + MyNums[i]
    NEXT i
    MyIndex = Index
    Mean = MySum / MyIndex
    Percent = (Hit / MyIndex) * 100
END SUB

'-----------
SUB CALCULATE_SD
'-----------
    LOCAL i
    FOR i = 1 TO Index
        Square = Square + POW((Mean - MyNums[i]), 2)
    NEXT i
    Square = Square / MyIndex
    SD = SQR(Square)
END SUB

'-----------
SUB SHOW_RESULTS
'-----------
    PRINT NL$, MyLine$
    PRINT "Randomness test"
    PRINT MyLine$
    PRINT "Probability   : ", Prob, "%"
    PRINT "Iterations    : ", Index
    PRINT "Hits          : ", Hit
    PRINT "Misses        : ", ABS(Miss)
    PRINT "Percent hits  : ", Percent, "%"
    PRINT "Mean:         : ", Mean
    PRINT "SD            : ", SD
    PRINT MyLine$, NL$
END SUB

' *****************
' END SUBROUTINES
' *****************


' *****************
' MAIN
' *****************

GET_INPUT
INIT_VARS
POPULATE_ARRAY
CALCULATE_MEAN
CALCULATE_SD
SHOW_RESULTS
END

' *****************
' END MAIN
' *****************