Skip to content

Instantly share code, notes, and snippets.

@u1tnk
Created August 9, 2012 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u1tnk/3303071 to your computer and use it in GitHub Desktop.
Save u1tnk/3303071 to your computer and use it in GitHub Desktop.
it "normal" do
a = [{id: 1, weight:1}, {id:2, weight: 3}, {id:3, weight:nil}]
check_count = 10
check_count.times do
sample_count = 1000
counts = {1 => 0, 2 => 0}
sample_count.times do
counts[a.sample_with_weight[:id]] += 1
end
(counts[1] < counts[2]).should be_true
(counts[1] + counts[2]).should == sample_count
end
end
it "weightを取る属性名を指定" do
a = [{id: 1, hoge_weight:1}, {id:2, hoge_weight: 2}]
check_count = 10
check_count.times do
sample_count = 1000
counts = {1 => 0, 2 => 0}
sample_count.times do
counts[a.sample_with_weight(:hoge_weight)[:id]] += 1
end
(counts[1] < counts[2]).should be_true
(counts[1] + counts[2]).should == sample_count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment