'*********************************************************
'* Math.bi                                     6/12/2021
'*
'* Basic math stuff I use in my games
'*********************************************************



'*********************************************************
'* PercentChance
'*
'* Has a 'Percentage' chance to return 1 otherwise 0
'*********************************************************

FUNCTION PercentChance (Percentage)
    PercentChance = 0
    Roll = RND * 100
    IF Roll <= Percentage THEN PercentChance = 1
END FUNCTION


'*********************************************************
'* Randomly Generates a number
'*
'* BaseNum - Limit of generator
'* Add - What to add to number after it is generated
'* Sign$ "+" Generates positive number
'*       "-" Generates negative number
'*       "~" 50/50 chance to be either positive or negative
'* Exclude - Number to exclude
'**********************************************************
FUNCTION RandomGen (BaseNum, Add, Sign$, Exclude)
    DO
        tmp = (RND * BaseNum) + Add
        IF tmp <> Exclude THEN EXIT DO
        Count = Count + 1
        IF Count = 500 THEN ERROR 99
    LOOP
    IF Sign$ = "~" AND PercentChance(50) = 1 THEN Sign$ = "-"

    IF Sign$ = "-" THEN
        RandomGen = tmp * -1
    ELSE
        RandomGen = tmp
    END IF
END FUNCTION


