probability 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +74 -0
- data/Rakefile +7 -0
- data/lib/probability.rb +6 -0
- data/lib/probability/core_ext/integer.rb +24 -0
- data/lib/probability/version.rb +3 -0
- data/probability.gemspec +19 -0
- data/spec/probability_spec.rb +28 -0
- data/spec/spec_helper.rb +7 -0
- metadata +83 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-fd -c
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ariejan de Vroom
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Probability [](http://travis-ci.org/ariejan/probability)
|
2
|
+
|
3
|
+
Probability gives you an easy way to perform code only every so
|
4
|
+
often, based on your input.
|
5
|
+
|
6
|
+
This is great for testing purposes or in games.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'probability'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install probability
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Usage is pretty simple. Let's say you want to get a 33% chance at something.
|
25
|
+
This means a 3 out of 10 (or 33 out of 100) chance.
|
26
|
+
|
27
|
+
3.in(10) => true # About 33% of the time
|
28
|
+
3.in(10) => false # About 67% of the time
|
29
|
+
|
30
|
+
You may also supply a block, which is only executed when the
|
31
|
+
probability conditions is true.
|
32
|
+
|
33
|
+
3.in(10) do
|
34
|
+
puts "w00t - 33% chance and I got it!"
|
35
|
+
end
|
36
|
+
|
37
|
+
Probability uses SecureRandom in ruby-1.9 to generate random values.
|
38
|
+
|
39
|
+
## Running specs
|
40
|
+
|
41
|
+
$ git clone git://github.com/ariejan/probability.git
|
42
|
+
$ bundle install
|
43
|
+
$ rake spec
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
Copyright (c) 2012 Ariejan de Vroom
|
56
|
+
|
57
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
58
|
+
a copy of this software and associated documentation files (the
|
59
|
+
"Software"), to deal in the Software without restriction, including
|
60
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
61
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
62
|
+
permit persons to whom the Software is furnished to do so, subject to
|
63
|
+
the following conditions:
|
64
|
+
|
65
|
+
The above copyright notice and this permission notice shall be
|
66
|
+
included in all copies or substantial portions of the Software.
|
67
|
+
|
68
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
69
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
70
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
71
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
72
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
73
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
74
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/probability.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
class Integer
|
4
|
+
# Probability of integer in number.
|
5
|
+
#
|
6
|
+
# 1.in(10) => true # about 10% of the time
|
7
|
+
# 1.in(10) => false # about 90% of the time
|
8
|
+
#
|
9
|
+
# You may also supply a block, which is only executed
|
10
|
+
# if when the condition is true
|
11
|
+
#
|
12
|
+
# 3.in(10) do
|
13
|
+
# # Do something 33% of the time
|
14
|
+
# end
|
15
|
+
def in(number, &block)
|
16
|
+
return false if number <= 0
|
17
|
+
|
18
|
+
threshold = self / number.to_f
|
19
|
+
result = SecureRandom.random_number(0) <= threshold
|
20
|
+
|
21
|
+
return yield if result && block_given?
|
22
|
+
result
|
23
|
+
end
|
24
|
+
end
|
data/probability.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/probability/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Ariejan de Vroom"]
|
6
|
+
gem.email = ["ariejan@ariejan.net"]
|
7
|
+
gem.description = %q{A simple extension to ruby to use probability in your apps. Great for games and stuff.}
|
8
|
+
gem.summary = %q{Add probability to your app.}
|
9
|
+
gem.homepage = "https://github.com/ariejan/probability"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "probability"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Probability::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec", "~> 2.10.0"
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
|
2
|
+
|
3
|
+
describe "Probability for Integer" do
|
4
|
+
context "1.in(1)" do
|
5
|
+
subject { 1.in(1) }
|
6
|
+
specify { should be_true }
|
7
|
+
end
|
8
|
+
|
9
|
+
context "0.in(1)" do
|
10
|
+
subject { 0.in(1) }
|
11
|
+
specify { should be_false }
|
12
|
+
end
|
13
|
+
|
14
|
+
context "3.in(0)" do
|
15
|
+
subject { 3.in(0) }
|
16
|
+
specify { should be_false }
|
17
|
+
end
|
18
|
+
|
19
|
+
context "with blocks" do
|
20
|
+
it "yields the block for 1.in(1)" do
|
21
|
+
expect { |b| 1.in(1, &b) }.to yield_with_no_args
|
22
|
+
end
|
23
|
+
|
24
|
+
it "does not yield the block for 0.in(1)" do
|
25
|
+
expect { |b| 0.in(1, &b) }.to_not yield_with_no_args
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: probability
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ariejan de Vroom
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.10.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.10.0
|
30
|
+
description: A simple extension to ruby to use probability in your apps. Great for
|
31
|
+
games and stuff.
|
32
|
+
email:
|
33
|
+
- ariejan@ariejan.net
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- .rspec
|
40
|
+
- .travis.yml
|
41
|
+
- Gemfile
|
42
|
+
- LICENSE
|
43
|
+
- README.md
|
44
|
+
- Rakefile
|
45
|
+
- lib/probability.rb
|
46
|
+
- lib/probability/core_ext/integer.rb
|
47
|
+
- lib/probability/version.rb
|
48
|
+
- probability.gemspec
|
49
|
+
- spec/probability_spec.rb
|
50
|
+
- spec/spec_helper.rb
|
51
|
+
homepage: https://github.com/ariejan/probability
|
52
|
+
licenses: []
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
hash: 4343454353977698419
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
hash: 4343454353977698419
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.23
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Add probability to your app.
|
81
|
+
test_files:
|
82
|
+
- spec/probability_spec.rb
|
83
|
+
- spec/spec_helper.rb
|