Module: Sass::Script::Functions::RandomSass
- Included in:
- Sass::Script::Functions
- Defined in:
- lib/random_sass.rb
Instance Method Summary collapse
-
#assert_same_unit(value1, value2, name = nil) ⇒ Object
A couple of handy assert for type validation.
- #assert_unitless(value, name = nil) ⇒ Object
-
#normal(p_mean, p_std) ⇒ Object
Returns a random number belonging to the set N(p_mean,p_std).
-
#uniform(p_min, p_max) ⇒ Object
Returns a random number belonging to the set U(p_min,p_max).
Instance Method Details
#assert_same_unit(value1, value2, name = nil) ⇒ Object
A couple of handy assert for type validation.
42 43 44 45 46 47 |
# File 'lib/random_sass.rb', line 42 def assert_same_unit(value1, value2, name = nil) return if value1.unit_str == value2.unit_str err = "#{value1} has different units than #{value2}" err = "$#{name}: " + err if name raise ArgumentError.new(err) end |
#assert_unitless(value, name = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/random_sass.rb', line 49 def assert_unitless(value, name = nil) assert_type value, :Number return if value.unitless? err = "#{value} is not unitless" err = "$#{name}: " + err if name raise ArgumentError.new(err) end |
#normal(p_mean, p_std) ⇒ Object
Returns a random number belonging to the set N(p_mean,p_std)
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/random_sass.rb', line 27 def normal(p_mean,p_std) assert_type p_mean, :Number assert_unitless p_std u = Random.rand(100000)/100000.0 v = Random.rand(100000)/100000.0 z = Math.sqrt(-2*Math.log(u)) * Math.cos(2*Math::PI*v) out_value = p_std.value * z + p_mean.value Sass::Script::Number.new(out_value,p_mean.numerator_units,p_mean.denominator_units) end |
#uniform(p_min, p_max) ⇒ Object
Returns a random number belonging to the set U(p_min,p_max)
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/random_sass.rb', line 12 def uniform(p_min,p_max) assert_type p_min, :Number assert_type p_max, :Number assert_same_unit p_max, p_min n_max = [p_max.value,p_min.value].max n_min = [p_max.value,p_min.value].min out_value = n_min + Random.rand(n_max-n_min) Sass::Script::Number.new(out_value,p_max.numerator_units,p_max.denominator_units) end |