composite_rng 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +49 -0
- data/composite_rng.gemspec +31 -0
- data/lib/composite_rng.rb +32 -0
- data/lib/composite_rng/version.rb +4 -0
- data/reek.txt +2 -0
- data/tests/composite_rng_tests.rb +39 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YTY0YTA0OWM2M2I1MDQ0YTRkNjdkMDU0YmY0OWVhMGE0NDZkMTgwZg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
M2U5MzVlN2UzOWEwNmFiZjU5YmIyZGIyNjlhOTdmN2Q0YWI2ZWViOA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDExOGI0YjNlMTM0Njc3YzZhYjA5NDBmZTg0Njc2NDA4ZjBkMjY1ZDE0MDcw
|
10
|
+
MmM4ZTViNzI4YWVmN2NkM2NmYzM0ZjRlZGNlNjI2MzM3ZTQ2YmI1NmI5NTMy
|
11
|
+
YjRmYTI4YjZhY2FkNDhmY2JlM2ViNGUzNDZiZWY2Y2RhNThiMGQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZWViMTI5YWU5MDg2MGFkNzg5NDIxY2EwOTkyYWM3YjhhNWI0NmFjZTQ4MDM4
|
14
|
+
YjhkZjFiYjAxMDNmOWU3ZWI3MWE0NmNlMGY0MGQ2NjJkOWE4ZWFjYjFmY2E3
|
15
|
+
ZDE2YWExOWUxOTlmYTI1ODdkYWRjNTNjMDI0YzdmZGVjOWY1OTA=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Peter Camilleri
|
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,63 @@
|
|
1
|
+
# CompositeRng
|
2
|
+
|
3
|
+
The composite (psuedo) random number generator is a container for two other
|
4
|
+
(psuedo) random number generators. By working together, these create higher
|
5
|
+
quality random number streams than either could by itself.
|
6
|
+
|
7
|
+
The two generators do NOT need to be the same type. While not a good idea,
|
8
|
+
they may even be the same instance, or the same type with the same seed, but
|
9
|
+
this negates the advantage of compositing them.
|
10
|
+
|
11
|
+
The generators used most comply to the following duck characteristics:
|
12
|
+
|
13
|
+
rand(max) - This method must compute a random integer from 0...max
|
14
|
+
|
15
|
+
That's all! The following shows how this could work:
|
16
|
+
|
17
|
+
parent = Random.new #Get the built in Mersenne Twister MT19937 PRNG
|
18
|
+
child = Random.new
|
19
|
+
composite = CompositeRng.new(parent, child)
|
20
|
+
# ...
|
21
|
+
dice_roll = composite(6)
|
22
|
+
# ...
|
23
|
+
|
24
|
+
It is also possible to use the default PRNG as follows:
|
25
|
+
|
26
|
+
parent = Object.new
|
27
|
+
child = Random.new
|
28
|
+
composite = CompositeRng.new(parent, child)
|
29
|
+
# ...
|
30
|
+
dice_roll = composite(6)
|
31
|
+
# ...
|
32
|
+
|
33
|
+
This is because the default PRNG exists as methods of Object. Note that (as
|
34
|
+
far as I can tell) only one instance of that PRNG exists, so it should only
|
35
|
+
be used as parent or child but never both!.
|
36
|
+
|
37
|
+
Most PRNGs provide more resources beyond the basic "rand" method. Access to
|
38
|
+
these other methods is provided by using the churn method. An example,
|
39
|
+
accessing the bytes method of the Random class follows:
|
40
|
+
|
41
|
+
crazy_string = composite.churn.bytes(22) # Get a string of 22 random chars.
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
|
45
|
+
Add this line to your application's Gemfile:
|
46
|
+
|
47
|
+
gem 'composite_rng'
|
48
|
+
|
49
|
+
And then execute:
|
50
|
+
|
51
|
+
$ bundle
|
52
|
+
|
53
|
+
Or install it yourself as:
|
54
|
+
|
55
|
+
$ gem install composite_rng
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
|
6
|
+
#Run the fOOrth unit test suite.
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
#List out all the test files.
|
9
|
+
t.test_files = FileList['tests/**/*.rb']
|
10
|
+
t.verbose = false
|
11
|
+
end
|
12
|
+
|
13
|
+
#Generate internal documentation with rdoc.
|
14
|
+
RDoc::Task.new do |rdoc|
|
15
|
+
rdoc.rdoc_dir = "rdoc"
|
16
|
+
|
17
|
+
#List out all the files to be documented.
|
18
|
+
rdoc.rdoc_files.include("lib/**/*.rb", "license.txt", "README.md")
|
19
|
+
|
20
|
+
#Make all access levels visible.
|
21
|
+
rdoc.options << '--visibility' << 'private'
|
22
|
+
|
23
|
+
#Set a title.
|
24
|
+
rdoc.options << '--title' << 'CompositeRng Docs'
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Run a scan for smelly code!"
|
29
|
+
task :reek do |t|
|
30
|
+
`reek --no-color lib > reek.txt`
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "Fire up an IRB session with composite_rng preloaded."
|
34
|
+
task :console do
|
35
|
+
require 'irb'
|
36
|
+
require 'irb/completion'
|
37
|
+
|
38
|
+
require './lib/composite_rng'
|
39
|
+
puts "Starting an IRB console for composite_rng."
|
40
|
+
|
41
|
+
ARGV.clear
|
42
|
+
IRB.start
|
43
|
+
end
|
44
|
+
|
45
|
+
desc "What version of the composite_rng is this?"
|
46
|
+
task :vers do |t|
|
47
|
+
puts
|
48
|
+
puts "CompositeRng version = #{CompositeRng::VERSION}"
|
49
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'composite_rng/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "composite_rng"
|
8
|
+
spec.version = CompositeRng::VERSION
|
9
|
+
spec.authors = ["Peter Camilleri"]
|
10
|
+
spec.email = ["peter.c.camilleri@gmail.com"]
|
11
|
+
spec.description = "A composable random number generator."
|
12
|
+
spec.summary = "A composable random number generation class."
|
13
|
+
spec.homepage = "http://teuthida-technologies.com/"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
raw_list = `git ls-files`.split($/)
|
17
|
+
raw_list = raw_list.keep_if {|entry| !entry.start_with?("docs") }
|
18
|
+
|
19
|
+
spec.files = raw_list
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.required_ruby_version = '>= 1.9.3'
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
spec.add_development_dependency 'reek', "~> 1.3.8"
|
29
|
+
spec.add_development_dependency 'minitest', "~> 4.7.5"
|
30
|
+
spec.add_development_dependency 'rdoc', "~> 4.0.1"
|
31
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "composite_rng/version"
|
2
|
+
|
3
|
+
#A class of random number generators that work by nesting other PRNGs.
|
4
|
+
class CompositeRng
|
5
|
+
|
6
|
+
#Create a composite PRNG
|
7
|
+
#<br>Parameters
|
8
|
+
#* parent - the starting PRNG
|
9
|
+
#* child - the progeny PRNG
|
10
|
+
#* churn - the amount of churning done.
|
11
|
+
def initialize(parent, child, churn=16)
|
12
|
+
@parent, @child, @churn = parent, child, churn
|
13
|
+
end
|
14
|
+
|
15
|
+
#An access point for random numbers.
|
16
|
+
#<br>Parameters
|
17
|
+
#* max - the range of the numbers to be created.
|
18
|
+
#<br>Returns
|
19
|
+
#* a random value generated according to the max parameter.
|
20
|
+
def rand(max=0)
|
21
|
+
churn.rand(max)
|
22
|
+
end
|
23
|
+
|
24
|
+
#Stir the soup!
|
25
|
+
#<br>Returns
|
26
|
+
#* a churned random number generator.
|
27
|
+
def churn
|
28
|
+
(1 + @parent.rand(@churn)).times {@child.rand(256)}
|
29
|
+
@child
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/reek.txt
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../lib/composite_rng'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
#Test the monkey patches applied to the Object class.
|
7
|
+
class CompositeRngTester < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
#Special initialize to track rake progress.
|
10
|
+
def initialize(*all)
|
11
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
12
|
+
|
13
|
+
if $do_this_only_one_time != __FILE__
|
14
|
+
puts
|
15
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
16
|
+
$do_this_only_one_time = __FILE__
|
17
|
+
end
|
18
|
+
|
19
|
+
super(*all)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_that_it_creates_dice_rolls
|
23
|
+
prng = CompositeRng.new(Random.new, Random.new)
|
24
|
+
|
25
|
+
100.times do
|
26
|
+
assert((0...6) === prng.rand(6))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_random_string
|
31
|
+
prng = CompositeRng.new(Random.new, Random.new)
|
32
|
+
|
33
|
+
rs = prng.churn.bytes(10)
|
34
|
+
|
35
|
+
assert(rs.is_a?(String))
|
36
|
+
assert_equal(10, rs.length)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: composite_rng
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Camilleri
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: reek
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.3.8
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.3.8
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.7.5
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.7.5
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rdoc
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.0.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.0.1
|
83
|
+
description: A composable random number generator.
|
84
|
+
email:
|
85
|
+
- peter.c.camilleri@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- composite_rng.gemspec
|
96
|
+
- lib/composite_rng.rb
|
97
|
+
- lib/composite_rng/version.rb
|
98
|
+
- reek.txt
|
99
|
+
- tests/composite_rng_tests.rb
|
100
|
+
homepage: http://teuthida-technologies.com/
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 1.9.3
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.1.4
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: A composable random number generation class.
|
124
|
+
test_files: []
|
125
|
+
has_rdoc:
|