Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Miscellaneous handy goodies
#1
Well, damn.

I had a very nicely formatted post all queued up and the browser crashed when I right clicked on a word to check its definition and spelling in this text area.

Anyway.

I will return to this to explain but mostly this is self-explanatory stuff.

Code: (Select All)
' For concatenating integers and stripping spaces
FUNCTION n$ (integ%)
    n$ = _TRIM$(STR$(integ%))
END FUNCTION

' For showing friendly versions of my boolean constants
FUNCTION b$ (integ%)
    IF integ% = -1 THEN
        b$ = "TRUE"
    ELSEIF integ% = 0 THEN
        b$ = "FALSE"
    ENDIF
END FUNCTION

' For concatenating longs and stripping spaces
FUNCTION ln$ (longval!)
    ln$ = _TRIM$(STR$(longval!))
END FUNCTION


' For incrementing integers - x=inc(1) takes up 3 more chars than x=x+1 but inc is a bit easier for me to read.
FUNCTION inc% (value%)
    inc% = value% + 1
END FUNCTION

' Same as above but decrement
FUNCTION dec% (value%)
    dec% = value% - 1
END FUNCTION

' Inverting int
FUNCTION inv% (value%)
    inv% = value% * -1
END FUNCTION

' Force int to be not less than min
FUNCTION min% (value%, minimum%)
    IF value% < minimum% THEN value% = minimum%
    min% = value%
END FUNCTION

' Force int to be not more than max
FUNCTION max% (value%, maximum%)
    IF value% > maximum% THEN value% = maximum%
    max% = value%
END FUNCTION

' Force int to be between a min and a max, when greater - clamp it between min and max
FUNCTION clamp% (value%, minimum%, maximum%)
    IF value% > maximum% THEN
        clamp% = maximum%
    ELSEIF value% < minimum% THEN
        clamp% = minimum%
    ELSE
        clamp% = value%
    END IF
END FUNCTION

' Determine if a int is in range of a min and a max
FUNCTION in_range% (value%, minimum%, maximum%)
    IF value% >= minimum% AND value% <= maximum% THEN
        in_range% = TRUE
    ELSE
        in_range% = FALSE
    END IF
END FUNCTION

' Randomize the sign of an int
FUNCTION rand_sign% ()
    DIM r AS INTEGER
    r% = -1 + INT(RND*2)
    IF r% = 0 THEN r% = 1
    rand_sign% = r%
END FUNCTION

' Create a random integer between min and max
FUNCTION rand_in_range% (minimum%, maximum%)
    rand_in_range% = INT(RND * (maximum% - minimum% + 1)) + 1
END FUNCTION

' Randomly choose an int from an array of ints
FUNCTION rand_int_choice% (arr_choices%())
    DIM AS INTEGER minimum, maximum
    minimum% = LBOUND(arr_choices%) : maximum% = UBOUND(arr_choices%)
    rand_int_choice% = arr_choices%(rand_in_range(minimum%, maximum%))
END FUNCTION

' Randomly choose a string from an array of strings
FUNCTION rand_str_choice$ (arr_choices$())
    DIM AS INTEGER minimum, maximum
    minimum% = LBOUND(arr_choices$) : maximum% = UBOUND(arr_choices$)
    rand_str_choice$ = arr_choices$(rand_in_range(minimum%, maximum%))
END FUNCTION
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply


Messages In This Thread
Miscellaneous handy goodies - by grymmjack - 12-28-2022, 01:44 AM
RE: Miscellaneous handy goodies - by grymmjack - 12-28-2022, 01:46 AM
RE: Miscellaneous handy goodies - by SMcNeill - 12-28-2022, 03:55 AM
RE: Miscellaneous handy goodies - by madscijr - 12-28-2022, 11:56 PM
RE: Miscellaneous handy goodies - by OldMoses - 12-28-2022, 01:41 PM
RE: Miscellaneous handy goodies - by mdijkens - 12-28-2022, 03:37 PM
RE: Miscellaneous handy goodies - by mnrvovrfc - 03-27-2023, 06:22 PM
RE: Miscellaneous handy goodies - by aurel - 04-02-2023, 05:01 PM



Users browsing this thread: 1 Guest(s)