Guest User

Untitled

a guest
Jun 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. require 'spec_helper'
  2.  
  3. describe RandomSource do
  4. let(:source) { RandomSource.new }
  5. let(:random_numbers) { source.random_numbers(sample_size) }
  6.  
  7. context "with a small sample size" do
  8. let(:sample_size) { 100 }
  9.  
  10. subject { random_numbers }
  11.  
  12. it { should have(sample_size).numbers }
  13. it { should be_all { |n| n >= 1 } }
  14. it { should be_all { |n| n <= 6 } }
  15. end
  16.  
  17. context "with a large sample size" do
  18. let(:sample_size) { 100000 }
  19. let(:expected_bin_size) { sample_size / 6.0 }
  20.  
  21. subject { count_occurrences(random_numbers) }
  22.  
  23. it { should have(6).bins }
  24.  
  25. it "is randomly distributed" do
  26. subject.each do |n, count|
  27. count.should be_within_percent(expected_bin_size, 5)
  28. end
  29. end
  30.  
  31. def count_occurrences(numbers)
  32. result = Hash.new { |h, k| h[k] = 0 }
  33. numbers.each do |n| result[n] += 1 end
  34. result
  35. end
  36. end
  37. end
Add Comment
Please, Sign In to add comment