class Chaos::Chaos
- Chaos::Chaos
- Reference
- Object
Defined in:
chaos.crchaos/types.cr
Constant Summary
-
DEFAULT_PROBABILITY =
0.5
-
Default probability
Instance Method Summary
-
#chaos(variable : Nil) : Nil
Nil
-
#chaos(variable : Bool) : Bool
Returns a chaotic boolean value based on the given probability.
-
#chaos(variable : Int8) : Int8
Returns a chaotic integer Int8 value based on the given probability.
-
#chaos(variable : Int16) : Int16
Returns a chaotic integer Int16 value based on the given probability.
-
#chaos(variable : Int32) : Int32
Returns a chaotic integer Int32 value based on the given probability.
-
#chaos(variable : Int64) : Int64
Returns a chaotic integer Int64 value based on the given probability.
-
#chaos(variable : Int128) : Int128
Returns a chaotic integer Int128 value based on the given probability.
-
#chaos(variable : UInt8) : UInt8
Returns a chaotic integer UInt8 value based on the given probability.
-
#chaos(variable : UInt16) : UInt16
Returns a chaotic integer UInt16 value based on the given probability.
-
#chaos(variable : UInt32) : UInt32
Returns a chaotic integer UInt32 value based on the given probability.
-
#chaos(variable : UInt64) : UInt64
Returns a chaotic integer UInt64 value based on the given probability.
-
#chaos(variable : UInt128) : UInt128
Returns a chaotic integer UInt128 value based on the given probability.
-
#chaos(variable : Float32) : Float32
Returns a chaotic float Float32 value based on the given probability.
-
#chaos(variable : Float64) : Float64
Returns a chaotic float Float64 value based on the given probability.
-
#chaos(variable : Char) : Char
Returns a chaotic char value based on the given probability.
-
#chaos(variable : String) : String
Returns a chaotic string value based on the given probability.
-
#chaos(variable : Array) : Array
Returns a chaotic array value based on the given probability.
-
#chaos(variable : Range) : Range
Returns a chaotic range value based on the given probability.
-
#chaos(value)
Generic unhandled type
-
#probability : Float64
Getter for the current probability
-
#probability=(value : Float64)
Setter for the new probability
-
#reset_probability
Resets probability to its default value
Instance Method Detail
Returns a chaotic boolean value based on the given probability.
This method takes a Bool
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns the opposite value of the input variable
.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(true) # => random value of type Bool based on the probability
Returns a chaotic integer Int8 value based on the given probability.
This method takes an Int8
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 8-bit integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(8_i8) # => random value of type Int8 based on the probability
Returns a chaotic integer Int16 value based on the given probability.
This method takes an Int16
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 16-bit integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(16_i16) # => random value of type Int16 based on the probability
Returns a chaotic integer Int32 value based on the given probability.
This method takes an Int32
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 32-bit integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(32_i32) # => random value of type Int32 based on the probability
Returns a chaotic integer Int64 value based on the given probability.
This method takes an Int64
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 64-bit integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(64_i64) # => random value of type Int64 based on the probability
Returns a chaotic integer Int128 value based on the given probability.
This method takes an Int128
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 128-bit integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(128_i128) # => random value of type Int128 based on the probability
Returns a chaotic integer UInt8 value based on the given probability.
This method takes an UInt8
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 8-bit unsigned integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(8_u8) # => random value of type UInt8 based on the probability
Returns a chaotic integer UInt16 value based on the given probability.
This method takes an UInt16
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 16-bit unsigned integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(16_u16) # => random value of type UInt16 based on the probability
Returns a chaotic integer UInt32 value based on the given probability.
This method takes an UInt32
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 32-bit unsigned integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(32_u32) # => random value of type UInt32 based on the probability
Returns a chaotic integer UInt64 value based on the given probability.
This method takes an UInt64
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 64-bit unsigned integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(64_u64) # => random value of type UInt64 based on the probability
Returns a chaotic integer UInt128 value based on the given probability.
This method takes an UInt128
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 128-bit unsigned integer range.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(128_u128) # => random value of type UInt128 based on the probability
Returns a chaotic float Float32 value based on the given probability.
This method takes a Float32
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 0.0 and 1.0 on 32-bit float type.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(32.0_f32) # => random value of type Float32 based on the probability
Returns a chaotic float Float64 value based on the given probability.
This method takes a Float64
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value between 0.0 and 1.0 on 64-bit float type.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos(64.0_f64) # => random value of type Float64 based on the probability
Returns a chaotic char value based on the given probability.
This method takes a Char
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value of a valid char.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.chaos('a') # => random value of type Char based on the probability
Returns a chaotic string value based on the given probability.
This method takes a String
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a chaotic string based on the input variable
.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
chaos.chaos("abcdefghi") # => chaotic string based on the probability
Returns a chaotic array value based on the given probability.
This method takes an Array
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns the reversed value of the input variable
.
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
chaos.chaos([1, 2, 3, 4, 5]) # => shuffled array based on the probability
Returns a chaotic range value based on the given probability.
This method takes a Range
variable as input and returns the same variable unless
a random value generated is less than the current chaos probability, in which case
it returns a random value of begin and end for the range.
Example passing a simple range:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
chaos.chaos(1..10) # => random range based on the probability
Example passing a range with different types:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
rangeBegin : Int128 = 1
rangeEnd : Int128 = 10
chaos.chaos(rangeBegin..rangeEnd) # => random range based on the probability
If the range have a value not handled by the method overloading, it will return the same range passed as input.
Example:
class Test
property name : String = "TEST"
end
object = Test.new
chaos = Chaos::Chaos.new
chaos.probability = 0.8
chaos.chaos(object..object) # => object..object
Generic unhandled type
This method is called when the type is not handled by the method overloading.
Returns the same variable passed as input.
Example:
class Test
property name : String = "TEST"
end
object = Test.new
chaos = Chaos::Chaos.new
chaos.probability = 0.8
chaos.chaos(object) # => object
Getter for the current probability
Example:
chaos = Chaos::Chaos.new
puts chaos.probability # => 0.5
Setter for the new probability
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.probability # => 0.8
Resets probability to its default value
Example:
chaos = Chaos::Chaos.new
chaos.probability = 0.8
puts chaos.probability # => 0.8
chaos.reset_probability
puts chaos.probability # => 0.5