PercentageValue
public class PercentageValue : RandomValue
A value generator class for random percentage values.
-
Returns a random
Double
between 0.0 and 1.0 with no limitations on decimal places on the output.Example:
// Usage let randomPercent = Random.percentage.value // internally, if the initial random value was 0.59483093929 // the output would be the same print(randomPercent) // 0.59483093929
Declaration
Swift
public var value: Double { get }
Return Value
Double
between 0.0 and 1.0 -
Returns a random
Double
between 0.0 and 1.0 rounded to the number of maximum decimal places.Example:
// Usage let randomPercent = Random.percentage.value(maxDecimalPlaces: 4) // internally, if the initial random value was 0.59483093929 // the output would be: 0.5948 print(randomPercent) // 0.5948
Note
If no value is specified formaxDecimalPlaces
the default value of10
will be usedDeclaration
Swift
public func value(roundedTo maxDecimalPlaces: Int) -> Double
Parameters
maxDecimalPlaces
an
Int
specifying how many decimal places to limit the returned value by.Return Value
Double
between 0.0 and 1.0 -
Dynamically returns a random
Double
value below the upper limit inRange
.Note
Note:
If a randomly generated value exceeds the upper,
range.max
is returned.
Declaration
Swift
public func withUpperLimit(of range: PercentageRange, maxDecimalPlaces: Int?) -> Double
Parameters
range
a
PercentageRange
configuration that defines the min and max range of the return value.maxDecimalPlaces
an optional configuration that limits the return value to the number of max places specified.
Return Value
Double
representing a random value between 0.0 andrange.max
-
A computed variable that returns random
Double
value between 0.0 and 1.0, respecting upper and lower percentage bound configuration.Precondition
Random.percentageRange
must be setNote
Note:
Rules:
- If a randomly generated value exceeds the max,
PercentageRange.max
is returned. - If a randomly generated value is less than the min,
PercentageRange.min
is returned.
Warning
If a value is not set with
Random.percentageRange = newValue
, before using this computed property, then 0.0 will be returned.Declaration
Swift
public var withUpperLimit: Double { get }
Return Value
Double
representing a random value within the configured range, ornil
if a range is not configured. - If a randomly generated value exceeds the max,
-
Dynamically returns a random Double value between 0 and 1 by
PercentageRange
provided.Note
Note:
If a randomly generated value exceeds the max,
PercentageRange.max
is returned.If a randomly generated value is less than the min,
PercentageRange.min
is returned.
Declaration
Swift
public func withinRange(_ range: PercentageRange) -> Double
Parameters
range
a
PercentageRange
configuration that defines the min and max of the rangeReturn Value
Double
representing a random value between min and max ofRange
-
Dynamically returns a random Double value between 0.0 and 1.0 by
PercentageRange
provided, and if configured, limited to the specified number of decminal places.Note
Note:
If a randomly generated value exceeds the max,
PercentageRange.max
is returned.If a randomly generated value is less than the min,
PercentageRange.min
is returned.If no maxDecimalPlaces are provided, no limitation will be applied to the return value.
Declaration
Swift
public func withinRange(_ range: PercentageRange, maxDecimalPlaces: Int?) -> Double
Parameters
range
a
PercentageRange
configuration that defines the min and max of the rangemaxDecimalPlaces
an optional configuration that limits the return value to the number of max places specified.
Return Value
Double
representing a random value between min and max ofRange
-
A computed variable that returns random Double value between 0 and 1, respecting upper and lower percentage bound configuration.
Precondition
Random.percentageRange
must be setNote
Note:
Rules:
- If a randomly generated value exceeds the max,
PercentageRange.max
is returned. - If a randomly generated value is less than the min,
PercentageRange.min
is returned.
Warning
If a value is not set with
Random.percentageRange
, 0.0 will be returned.Declaration
Swift
public var withinRange: Double { get }
Return Value
Double
representing a random value within the configured range, ornil
if a range is not configured. - If a randomly generated value exceeds the max,