Skip to content

Instantly share code, notes, and snippets.

@industrialsociety
Created January 26, 2024 06:28
Show Gist options
  • Save industrialsociety/098497b84d828126750e0dc3f5d8c55e to your computer and use it in GitHub Desktop.
Save industrialsociety/098497b84d828126750e0dc3f5d8c55e to your computer and use it in GitHub Desktop.
def generate_numbers
random_generator = Random.new
Array.new(100) { random_generator.rand(1..100) }
end
def read_existing_data(file_name)
# You can modify the file name by changing the 'file_name' variable
if File.exist?(file_name)
data = File.read(file_name).split("\n")
run_count = data[0].split.last.to_i
freq_table = data[2..-1].each_with_object(Hash.new(0)) do |line, counts|
number, count = line.split(':').map(&:to_i)
counts[number] += count
end
[run_count, freq_table]
else
[0, Hash.new(0)]
end
end
def update_frequency_table(numbers, existing_freq_table)
numbers.each_with_object(existing_freq_table) { |number, counts| counts[number] += 1 }
end
def write_data_to_file(run_count, freq_table, file_name)
# You can modify the file name by changing the 'file_name' variable
File.open(file_name, 'w') do |file|
file.puts "Test Run Count: #{run_count}"
file.puts "---- Cumulative Frequency ----"
freq_table.sort_by { |_, count| -count }.each do |number, count|
file.puts "#{number}: #{count}"
end
end
end
# Modify the number of runs by changing the 'num_runs' variable
num_runs = 1
output_file = '1results.txt' # You can change the output file name here
num_runs.times do
run_count, freq_table = read_existing_data(output_file)
numbers = generate_numbers
updated_freq_table = update_frequency_table(numbers, freq_table)
write_data_to_file(run_count + 1, updated_freq_table, output_file)
end
puts "Results updated in #{output_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment