random_outcome 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/.rvmrc +62 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +78 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/lib/simulator.rb +61 -0
- data/random_outcome.gemspec +69 -0
- data/spec/simulator_spec.rb +81 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/matchers/give_the_outcome.rb +28 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec01216c257498d7ec663d0cb3bb99868723eb17
|
4
|
+
data.tar.gz: 4ba6c9d3626524b12af29344d17194fce7943344
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 483bc08406ad70ce1d7ff93a2d3fb793ae3259977bfc2f03fc71e46122787543b5fbc8a748491013ab65cf9a6702edaba5b2841f2af33405ad0b561b39e4d706
|
7
|
+
data.tar.gz: 374f8fb4809a969595ca9f9552a5e0d7259d575eb4c159458eeaa54f429ab96c6428446979c1fa8425fa64fd8bce9401302e5c2e310a3e7c0a00a5861a7b9dab
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 2.0.0" > .rvmrc
|
9
|
+
environment_id="ruby-2.0.0-p247@random_outcome"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.22.14 (stable)" # 1.10.1 seems like a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
|
27
|
+
do
|
28
|
+
if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
|
29
|
+
then \. "${__hook}" || true
|
30
|
+
fi
|
31
|
+
done
|
32
|
+
unset __hook
|
33
|
+
if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
|
34
|
+
then
|
35
|
+
if [[ $- == *i* ]] # check for interactive shells
|
36
|
+
then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
|
37
|
+
" # show the user the ruby and gemset they are using in green
|
38
|
+
else printf "%b" "Using: $GEM_HOME
|
39
|
+
" # don't use colors in non-interactive shells
|
40
|
+
fi
|
41
|
+
fi
|
42
|
+
else
|
43
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
44
|
+
rvm --create "$environment_id" || {
|
45
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
46
|
+
return 1
|
47
|
+
}
|
48
|
+
fi
|
49
|
+
|
50
|
+
# If you use bundler, this might be useful to you:
|
51
|
+
# if [[ -s Gemfile ]] && {
|
52
|
+
# ! builtin command -v bundle >/dev/null ||
|
53
|
+
# builtin command -v bundle | GREP_OPTIONS="" \grep $rvm_path/bin/bundle >/dev/null
|
54
|
+
# }
|
55
|
+
# then
|
56
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
57
|
+
# gem install bundler
|
58
|
+
# fi
|
59
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
60
|
+
# then
|
61
|
+
# bundle install | GREP_OPTIONS="" \grep -vE '^Using|Your bundle is complete'
|
62
|
+
# fi
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.8.0"
|
10
|
+
gem "yard", "~> 0.7"
|
11
|
+
gem "rdoc", "~> 3.12"
|
12
|
+
gem "bundler", "~> 1.0"
|
13
|
+
gem "jeweler", "~> 1.8.7"
|
14
|
+
end
|
15
|
+
|
16
|
+
gem "activesupport"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (4.0.0)
|
5
|
+
i18n (~> 0.6, >= 0.6.4)
|
6
|
+
minitest (~> 4.2)
|
7
|
+
multi_json (~> 1.3)
|
8
|
+
thread_safe (~> 0.1)
|
9
|
+
tzinfo (~> 0.3.37)
|
10
|
+
addressable (2.3.5)
|
11
|
+
atomic (1.1.14)
|
12
|
+
builder (3.2.2)
|
13
|
+
diff-lcs (1.1.3)
|
14
|
+
faraday (0.8.8)
|
15
|
+
multipart-post (~> 1.2.0)
|
16
|
+
git (1.2.6)
|
17
|
+
github_api (0.10.1)
|
18
|
+
addressable
|
19
|
+
faraday (~> 0.8.1)
|
20
|
+
hashie (>= 1.2)
|
21
|
+
multi_json (~> 1.4)
|
22
|
+
nokogiri (~> 1.5.2)
|
23
|
+
oauth2
|
24
|
+
hashie (2.0.5)
|
25
|
+
highline (1.6.20)
|
26
|
+
httpauth (0.2.0)
|
27
|
+
i18n (0.6.5)
|
28
|
+
jeweler (1.8.8)
|
29
|
+
builder
|
30
|
+
bundler (~> 1.0)
|
31
|
+
git (>= 1.2.5)
|
32
|
+
github_api (= 0.10.1)
|
33
|
+
highline (>= 1.6.15)
|
34
|
+
nokogiri (= 1.5.10)
|
35
|
+
rake
|
36
|
+
rdoc
|
37
|
+
json (1.8.1)
|
38
|
+
jwt (0.1.8)
|
39
|
+
multi_json (>= 1.5)
|
40
|
+
minitest (4.7.5)
|
41
|
+
multi_json (1.8.2)
|
42
|
+
multi_xml (0.5.5)
|
43
|
+
multipart-post (1.2.0)
|
44
|
+
nokogiri (1.5.10)
|
45
|
+
oauth2 (0.9.2)
|
46
|
+
faraday (~> 0.8)
|
47
|
+
httpauth (~> 0.2)
|
48
|
+
jwt (~> 0.1.4)
|
49
|
+
multi_json (~> 1.0)
|
50
|
+
multi_xml (~> 0.5)
|
51
|
+
rack (~> 1.2)
|
52
|
+
rack (1.5.2)
|
53
|
+
rake (10.1.0)
|
54
|
+
rdoc (3.12.2)
|
55
|
+
json (~> 1.4)
|
56
|
+
rspec (2.8.0)
|
57
|
+
rspec-core (~> 2.8.0)
|
58
|
+
rspec-expectations (~> 2.8.0)
|
59
|
+
rspec-mocks (~> 2.8.0)
|
60
|
+
rspec-core (2.8.0)
|
61
|
+
rspec-expectations (2.8.0)
|
62
|
+
diff-lcs (~> 1.1.2)
|
63
|
+
rspec-mocks (2.8.0)
|
64
|
+
thread_safe (0.1.3)
|
65
|
+
atomic
|
66
|
+
tzinfo (0.3.38)
|
67
|
+
yard (0.8.7.2)
|
68
|
+
|
69
|
+
PLATFORMS
|
70
|
+
ruby
|
71
|
+
|
72
|
+
DEPENDENCIES
|
73
|
+
activesupport
|
74
|
+
bundler (~> 1.0)
|
75
|
+
jeweler (~> 1.8.7)
|
76
|
+
rdoc (~> 3.12)
|
77
|
+
rspec (~> 2.8.0)
|
78
|
+
yard (~> 0.7)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Nigel Lowry
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= random_outcome
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to random_outcome
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
+
* Fork the project.
|
10
|
+
* Start a feature/bugfix branch.
|
11
|
+
* Commit and push until you are happy with your contribution.
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2013 Nigel Lowry. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "random_outcome"
|
18
|
+
gem.homepage = "http://github.com/nigel-lowry/random_outcome"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Small Ruby gem for generating outcomes with specified probabilities}
|
21
|
+
gem.description = %Q{Generates outcomes with specified probabilities which could be useful for gaming. Uses the wheel of fortune method.}
|
22
|
+
gem.email = "nigel-lowry@ultra.eclipse.co.uk"
|
23
|
+
gem.authors = ["Nigel Lowry"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'yard'
|
42
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/simulator.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
|
3
|
+
# Generates outcomes with specified probabilities
|
4
|
+
class Simulator
|
5
|
+
|
6
|
+
# creates a new Simulator which will return the desired outcomes with the given probability
|
7
|
+
# @param outcome_to_probability [Hash<Symbol, Number>] hash of outcomes to their probability (represented as
|
8
|
+
# numbers between zero and one)
|
9
|
+
# @note raises errors if there is only one possible outcome (why bother using this if there's only one
|
10
|
+
# outcome?), if the probabilities don't total one (use Rationals if this proves problematic with rounding) or
|
11
|
+
# if any of the outcomes are impossible (why include them if they can never happen?)
|
12
|
+
def initialize outcome_to_probability
|
13
|
+
raise_if_only_have_one_outcome outcome_to_probability
|
14
|
+
raise_if_probabilities_total_isnt_one outcome_to_probability
|
15
|
+
raise_if_have_any_impossible_outcomes outcome_to_probability
|
16
|
+
raise_if_have_any_negative_probabilities outcome_to_probability
|
17
|
+
@probability_range_to_outcome = probability_range_to_outcome outcome_to_probability
|
18
|
+
end
|
19
|
+
|
20
|
+
# generate an outcome with the initialised probabilities
|
21
|
+
# @return [Symbol] symbol for outcome
|
22
|
+
def outcome
|
23
|
+
num = random_float_including_zero_and_excluding_one # don't inline
|
24
|
+
@probability_range_to_outcome.detect {|probability_range, _| num.in? probability_range }.last
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def raise_if_only_have_one_outcome outcome_to_probability
|
30
|
+
raise "only have one outcome" unless outcome_to_probability.many?
|
31
|
+
end
|
32
|
+
|
33
|
+
def raise_if_probabilities_total_isnt_one outcome_to_probability
|
34
|
+
raise "probabilities don't total one" unless outcome_to_probability.values.sum == 1.0
|
35
|
+
end
|
36
|
+
|
37
|
+
def raise_if_have_any_impossible_outcomes outcome_to_probability
|
38
|
+
raise "have an impossible outcome" if outcome_to_probability.has_value? 0.0
|
39
|
+
end
|
40
|
+
|
41
|
+
def raise_if_have_any_negative_probabilities outcome_to_probability
|
42
|
+
raise "have negative probability" if outcome_to_probability.values.any? {|probability| probability < 0.0 }
|
43
|
+
end
|
44
|
+
|
45
|
+
def random_float_including_zero_and_excluding_one
|
46
|
+
rand
|
47
|
+
end
|
48
|
+
|
49
|
+
def probability_range_to_outcome outcome_to_probability
|
50
|
+
range_to_outcome = {}
|
51
|
+
lower_bound = 0.0
|
52
|
+
|
53
|
+
outcome_to_probability.each do |outcome, probability|
|
54
|
+
upper_bound = lower_bound + probability
|
55
|
+
range_to_outcome.store lower_bound...upper_bound, outcome
|
56
|
+
lower_bound = upper_bound
|
57
|
+
end
|
58
|
+
|
59
|
+
range_to_outcome
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: random_outcome 0.1.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "random_outcome"
|
9
|
+
s.version = "0.1.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.authors = ["Nigel Lowry"]
|
13
|
+
s.date = "2013-10-27"
|
14
|
+
s.description = "Generates outcomes with specified probabilities which could be useful for gaming. Uses the wheel of fortune method."
|
15
|
+
s.email = "nigel-lowry@ultra.eclipse.co.uk"
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".rspec",
|
23
|
+
".rvmrc",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"LICENSE.txt",
|
27
|
+
"README.rdoc",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"lib/simulator.rb",
|
31
|
+
"random_outcome.gemspec",
|
32
|
+
"spec/simulator_spec.rb",
|
33
|
+
"spec/spec_helper.rb",
|
34
|
+
"spec/support/matchers/give_the_outcome.rb"
|
35
|
+
]
|
36
|
+
s.homepage = "http://github.com/nigel-lowry/random_outcome"
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = "2.1.5"
|
40
|
+
s.summary = "Small Ruby gem for generating outcomes with specified probabilities"
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
s.specification_version = 4
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
47
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
48
|
+
s.add_development_dependency(%q<yard>, ["~> 0.7"])
|
49
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
50
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
51
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.7"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
54
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
55
|
+
s.add_dependency(%q<yard>, ["~> 0.7"])
|
56
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
57
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
58
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
62
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
63
|
+
s.add_dependency(%q<yard>, ["~> 0.7"])
|
64
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
65
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
66
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Simulator do
|
4
|
+
|
5
|
+
context "p(A) = 10%, p(B) = 90%" do
|
6
|
+
before :all do
|
7
|
+
@simulator = Simulator.new(A: 0.1, B: 0.9)
|
8
|
+
end
|
9
|
+
|
10
|
+
subject { @simulator }
|
11
|
+
|
12
|
+
it { should give_the_outcome(:A).with_the_probability(0.1) }
|
13
|
+
it { should give_the_outcome(:B).with_the_probability(0.9) }
|
14
|
+
it { should give_the_outcome(:C).with_the_probability(0.0) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context "p(A) = 40%, p(B) = 60%" do
|
18
|
+
before :all do
|
19
|
+
@simulator = Simulator.new(A: 0.4, B: 0.6)
|
20
|
+
end
|
21
|
+
|
22
|
+
subject { @simulator }
|
23
|
+
|
24
|
+
it { should give_the_outcome(:A).with_the_probability(0.4) }
|
25
|
+
it { should give_the_outcome(:B).with_the_probability(0.6) }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "p(A) = 10%, p(B) = 20%, p(C) = 70%" do
|
29
|
+
before :all do
|
30
|
+
@simulator = Simulator.new(A: 0.1, B: 0.2, C: 0.7)
|
31
|
+
end
|
32
|
+
|
33
|
+
subject { @simulator }
|
34
|
+
|
35
|
+
it { should give_the_outcome(:A).with_the_probability(0.1) }
|
36
|
+
it { should give_the_outcome(:B).with_the_probability(0.2) }
|
37
|
+
it { should give_the_outcome(:C).with_the_probability(0.7) }
|
38
|
+
end
|
39
|
+
|
40
|
+
context "with no outcomes" do
|
41
|
+
it "will raise error" do
|
42
|
+
expect { Simulator.new({}) }.to raise_error
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "with only one outcome" do
|
47
|
+
it "will raise error" do
|
48
|
+
expect { Simulator.new(A: 1.0) }.to raise_error
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "with any probabilities less than 0.0" do
|
53
|
+
it "will raise error" do
|
54
|
+
expect { Simulator.new(A: 0.9, B: 0.2, C: -0.1) }.to raise_error
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "with any probabilities greater than or equal to 1.0" do
|
59
|
+
it "will raise error" do
|
60
|
+
expect { Simulator.new(A: 1.1, B: 0.1, C: -0.2) }.to raise_error
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "with probabilities summing to less than 1.0" do
|
65
|
+
it "will raise error" do
|
66
|
+
expect { Simulator.new(A: 0.1, B: 0.89) }.to raise_error
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "with probabilities summing to more than 1.0" do
|
71
|
+
it "will raise error" do
|
72
|
+
expect { Simulator.new(A: 0.1, B: 0.91) }.to raise_error
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "with an impossible event" do
|
77
|
+
it "will raise error" do
|
78
|
+
expect { Simulator.new(A: 0.1, B: 0.9, C: 0.0) }.to raise_error
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'simulator'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
RSpec::Matchers.define :give_the_outcome do |outcome|
|
2
|
+
match do |actual|
|
3
|
+
outcome_count = 0
|
4
|
+
trial_count = 100_000
|
5
|
+
|
6
|
+
trial_count.times do
|
7
|
+
outcome_count += 1 if actual.outcome == outcome
|
8
|
+
end
|
9
|
+
|
10
|
+
(outcome_count / trial_count.to_f).should be_within(0.01).of(@probability)
|
11
|
+
end
|
12
|
+
|
13
|
+
chain :with_the_probability do |probability|
|
14
|
+
@probability = probability
|
15
|
+
end
|
16
|
+
|
17
|
+
failure_message_for_should do |actual|
|
18
|
+
"expected that #{outcome} would occur with a probability of #{@probability}"
|
19
|
+
end
|
20
|
+
|
21
|
+
failure_message_for_should_not do |actual|
|
22
|
+
"expected that #{outcome} would not occur with a probability of #{@probability}"
|
23
|
+
end
|
24
|
+
|
25
|
+
description do
|
26
|
+
"occur with a probability of #{@probability}"
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: random_outcome
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nigel Lowry
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.8.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.8.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yard
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rdoc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: jeweler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.8.7
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.8.7
|
97
|
+
description: Generates outcomes with specified probabilities which could be useful
|
98
|
+
for gaming. Uses the wheel of fortune method.
|
99
|
+
email: nigel-lowry@ultra.eclipse.co.uk
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files:
|
103
|
+
- LICENSE.txt
|
104
|
+
- README.rdoc
|
105
|
+
files:
|
106
|
+
- .document
|
107
|
+
- .rspec
|
108
|
+
- .rvmrc
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.rdoc
|
113
|
+
- Rakefile
|
114
|
+
- VERSION
|
115
|
+
- lib/simulator.rb
|
116
|
+
- random_outcome.gemspec
|
117
|
+
- spec/simulator_spec.rb
|
118
|
+
- spec/spec_helper.rb
|
119
|
+
- spec/support/matchers/give_the_outcome.rb
|
120
|
+
homepage: http://github.com/nigel-lowry/random_outcome
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - '>='
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.1.5
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Small Ruby gem for generating outcomes with specified probabilities
|
144
|
+
test_files: []
|