Class: DumpCleaner::Cleanup::CleaningSteps::RandomizeNumber
- Defined in:
- lib/dump_cleaner/cleanup/cleaning_steps/randomize_number.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#crc32, #initialize, #raise_params_error
Constructor Details
This class inherits a constructor from DumpCleaner::Cleanup::CleaningSteps::Base
Instance Method Details
#run(difference_within: 1.0) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dump_cleaner/cleanup/cleaning_steps/randomize_number.rb', line 7 def run(difference_within: 1.0) random = Random.new(crc32) new_value = current_value.to_f + random.rand(difference_within.to_f * 2) - difference_within.to_f # keep sign to keep string length (warning: this skews the distribution of the random numbers) if (current_value.strip[0] == "-") && new_value.positive? || (current_value.strip[0] != "-") && new_value.negative? new_value *= -1 end decimal_places = current_value.split(".")[1].to_s.length epsilon = 10**-decimal_places clamped_value = new_value.clamp(current_value.to_f - difference_within + epsilon, current_value.to_f + difference_within - epsilon) step_context.current_value = format("%0#{current_value.length}.#{decimal_places}f", clamped_value) step_context end |