PHPSL Function:  Rand_Decimal( )

  • Rand_Decimal ($NumDigits=1, $RandSignFlag=FALSE)

    Dependencies: NONE


    This function simply generates a random negative or positive fractional value of any number of given number of decimals.  The returned value will always be between ±1 exclusive.

    Sometimes a function that simply returns a signed, random decimal value can be very useful.

    The $RandSignFlag argument, if set to TRUE, will randomize the numerical sign of the result.  If set to FALSE $($default$)$, the number will always be a positive value.  When indicated, the numerical sign probability is 50:50 or equally probable.  In other words, if this flag is set to TRUE, there is a 50:50 chance that the random decimal will be negative.

    This function can be used with arbitrary-precision BC arithmetic functions or with the standard fixed double-precision functions when signed random decimals may be needed.

    Double-Click to select ALL code

    Double-Click to select ALL code


    EXAMPLE call results:
    print Rand_Decimal(16); // =-->  0.1756870043197103
    print Rand_Decimal(29); // =-->  0.31989573754978402956837284624
    print Rand_Decimal(30, TRUE); // =--> +0.232433748845834204661646774503
    

Jay Tanner - 2024