fair_random 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fair_random.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 todesking
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.
@@ -0,0 +1,64 @@
1
+ # FairRandom
2
+
3
+ FairRandom is random number generator that outputs more 'fair' numer serquence than true random.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fair_random'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fair_random
18
+
19
+ ## Usage
20
+
21
+ ### FairRandom::Box
22
+
23
+ `FairRandom::Box.new(N, M)` generates numbers that in range 0 to N-1(incusive).
24
+ There is M numbers in the box and occurence of any number is same, M/N.
25
+ Each `next` called, box will remove a element and return it.
26
+ If box is empty, box contents will regenerate.
27
+
28
+ (NOTE: M % N should == 0)
29
+
30
+ ```ruby
31
+ # Example
32
+ require 'fair_random'
33
+
34
+ class FairCoinToss
35
+ def initialize
36
+ @rng = FairRandom::Box.new(2, 10)
37
+ end
38
+
39
+ # return 0 or 1
40
+ def toss!
41
+ @rng.next
42
+ end
43
+ end
44
+
45
+ #### Serialize state
46
+
47
+ You can serialize random state with to_poro/from_poro (with to_json or Marshal etc if needed).
48
+
49
+ ```ruby
50
+ other_instance = FairRandom::Box.from_poro(random.to_poro)
51
+ ```
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
60
+
61
+ ## Changes
62
+
63
+ # 0.0.1
64
+ * initial: FairRandom::Box
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fair_random/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fair_random"
8
+ spec.version = FairRandom::VERSION
9
+ spec.authors = ["todesking"]
10
+ spec.email = ["discommunicative@gmail.com"]
11
+ spec.description = %q{"Fair" random number generator}
12
+ spec.summary = %q{"Fair" random number generator}
13
+ spec.homepage = "https://github.com/todesking/fair_random"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rspec-given"
25
+ spec.add_development_dependency "count_by"
26
+ spec.add_development_dependency "1_as_identity_function"
27
+ end
@@ -0,0 +1,47 @@
1
+ require "fair_random/version"
2
+
3
+ module FairRandom
4
+ class Box
5
+ def initialize(element_type_count, box_capacity)
6
+ raise ArgumentError if box_capacity % element_type_count != 0
7
+ @element_type_count = element_type_count
8
+ @box_capacity = box_capacity
9
+ reset_contents
10
+ end
11
+
12
+ def next
13
+ reset_contents if @index >= @contents.size
14
+ c = @contents[@index]
15
+ @index += 1
16
+ c
17
+ end
18
+
19
+ # Convert to PORO(Plain Old Ruby Object) for serialize.
20
+ def to_poro
21
+ {
22
+ 'type_count' => @element_type_count,
23
+ 'box_capacity' => @box_capacity,
24
+ 'contents' => @contents,
25
+ }
26
+ end
27
+
28
+ def self.from_poro(poro)
29
+ instance = new(poro['type_count'], poro['box_capacity'])
30
+ instance.instance_eval do
31
+ @contents = poro['contents']
32
+ end
33
+ instance
34
+ end
35
+
36
+ private
37
+ def reset_contents
38
+ @contents = []
39
+ @index = 0
40
+ # type_index => count
41
+ @element_type_count.times do|type|
42
+ @contents.concat([type] * (@box_capacity / @element_type_count))
43
+ end
44
+ @contents = @contents.shuffle
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module FairRandom
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe FairRandom do
4
+ describe 'Box' do
5
+ Given(:box) { FairRandom::Box.new(element_type_count, box_capacity) }
6
+ context 'box size == type count' do
7
+ describe 'content count' do
8
+ Given(:box_capacity) { 4 }
9
+ Given(:element_type_count) { 2 }
10
+ When(:box_contents) { box_capacity.times.map { box.next } }
11
+ Then { box_contents.count_by(&1) == {0 => 2, 1 => 2} }
12
+ end
13
+ describe 'reset contents' do
14
+ Given(:box_capacity) { 4 }
15
+ Given(:element_type_count) { 2 }
16
+ When(:box_contents) { (box_capacity * 2).times.map { box.next } }
17
+ Then { box_contents.count_by(&1) == {0 => 4, 1 => 4} }
18
+ end
19
+ end
20
+ context 'box size MOD type count != 0' do
21
+ Given(:box_capacity) { 3 }
22
+ Given(:element_type_count) { 2 }
23
+ Then { expect{box}.to raise_error ArgumentError }
24
+ end
25
+ context 'serialize/deserialize with PORO' do
26
+ Given(:box_capacity) { 4 }
27
+ Given(:element_type_count) { 2 }
28
+ Given(:poro) { box.to_poro }
29
+ Given(:revived) { FairRandom::Box.from_poro(poro) }
30
+ When(:box_contents) { (box_capacity * 2).times.map { revived.next } }
31
+ Then { box_contents.count_by(&1) == {0 => 4, 1 => 4} }
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,8 @@
1
+ require 'rspec/given'
2
+ require 'count_by'
3
+ require '1_as_identity_function'
4
+ require_relative '../lib/fair_random.rb'
5
+
6
+ def scenario(*args)
7
+ context(*args)
8
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fair_random
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - todesking
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
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: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-given
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: count_by
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: 1_as_identity_function
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: ! '"Fair" random number generator'
111
+ email:
112
+ - discommunicative@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - LICENSE.txt
120
+ - README.md
121
+ - Rakefile
122
+ - fair_random.gemspec
123
+ - lib/fair_random.rb
124
+ - lib/fair_random/version.rb
125
+ - spec/fair_random_spec.rb
126
+ - spec/spec_helper.rb
127
+ homepage: https://github.com/todesking/fair_random
128
+ licenses:
129
+ - MIT
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 1.8.25
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: ! '"Fair" random number generator'
152
+ test_files:
153
+ - spec/fair_random_spec.rb
154
+ - spec/spec_helper.rb