Skip to content

Commit

Permalink
Add documentation for integer and float methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Laureano committed Nov 23, 2024
1 parent 28bc1d7 commit 84d809f
Showing 1 changed file with 156 additions and 12 deletions.
168 changes: 156 additions & 12 deletions src/chaos/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,206 @@ module Chaos
rand < @probability ? !variable : variable
end

# Integer int8
# 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
# ```
def chaos(variable : Int8) : Int8
rand < @probability ? (variable = Random.rand(-128_i8..127_i8).to_i8) : variable
end

# Integer int16
# 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
# ```
def chaos(variable : Int16) : Int16
rand < @probability ? (variable = Random.rand(-32_768_i16..32_767_i16).to_i16) : variable
end

# Integer int32
# 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
# ```
def chaos(variable : Int32) : Int32
rand < @probability ? (variable = Random.rand(-2_147_483_648_i32..2_147_483_647_i32).to_i32) : variable
end

# Integer int64
# 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
# ```
def chaos(variable : Int64) : Int64
rand < @probability ? (variable = Random.rand(-9_223_372_036_854_775_808_i64..9_223_372_036_854_775_807_i64).to_i64) : variable
end

# Integer int128
# 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
# ```
def chaos(variable : Int128) : Int128
rand < @probability ? (variable = Random.rand(-170_141_183_460_469_231_731_687_303_715_884_105_728_i128..170_141_183_460_469_231_731_687_303_715_884_105_727_i128).to_i128) : variable
end

# Integer uint8
# 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
# ```
def chaos(variable : UInt8) : UInt8
rand < @probability ? (variable = Random.rand(0_u8..255_u8).to_u8) : variable
end

# Integer uint16
# 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
# ```
def chaos(variable : UInt16) : UInt16
rand < @probability ? (variable = Random.rand(0_u16..65_535_u16).to_u16) : variable
end

# Integer uint32
# 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
# ```
def chaos(variable : UInt32) : UInt32
rand < @probability ? (variable = Random.rand(0_u32..4_294_967_295_u32).to_u32) : variable
end

# Integer uint64
# 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
# ```
def chaos(variable : UInt64) : UInt64
rand < @probability ? (variable = Random.rand(0_u64..18_446_744_073_709_551_615_u64).to_u64) : variable
end

# Integer uint128
# 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
# ```
def chaos(variable : UInt128) : UInt128
rand < @probability ? (variable = Random.rand(0_u128..340_282_366_920_938_463_463_374_607_431_768_211_455_u128).to_u128) : variable
end

# Float float32
# 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
# ```
def chaos(variable : Float32) : Float32
rand < @probability ? (variable = Random.rand(0.0...1.0).to_f32) : variable
end

# Float float64
# 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
# ```
def chaos(variable : Float64) : Float64
rand < @probability ? (variable = Random.rand(0.0...1.0).to_f64) : variable
end
Expand Down

0 comments on commit 84d809f

Please sign in to comment.