composite_rng 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/README.md +61 -21
- data/irbt.rb +27 -0
- data/lib/composite_rng/version.rb +1 -1
- data/lib/composite_rng.rb +1 -1
- data/{Rakefile → rakefile.rb} +1 -8
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c30e36b6d59c807536035ad3fdbaefa8dccb420
|
4
|
+
data.tar.gz: 3ff2d418c856aa7af2b76af17e5f9d3950caa6d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8282ef4f619b086e53fc1663cc843008b194f533baa72d0806d499d4ef4e3c5bdad1c50e9a49de39d1825d379c5275d697edc0c00dbe0275214ddfa2c185e7c4
|
7
|
+
data.tar.gz: 495bef03e18c3860c9e50cb023fe50e70b792d66f9d066ee830697e2c290d9668a0a273b4b7ba8b82d609862f1ca98d66bfddaa4c9ad355670b1777dd2120652
|
data/README.md
CHANGED
@@ -8,37 +8,68 @@ The two generators do NOT need to be the same type. While not a good idea,
|
|
8
8
|
they may even be the same instance, or the same type with the same seed, but
|
9
9
|
this negates the advantage of compositing them.
|
10
10
|
|
11
|
-
The generators used
|
11
|
+
The generators used must comply to the following duck characteristics:
|
12
12
|
|
13
13
|
rand(max) - This method must compute a random integer from 0...max
|
14
14
|
|
15
15
|
That's all! The following shows how this could work:
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
```ruby
|
17
|
+
parent = Random.new #Get the built in Mersenne Twister MT19937 PRNG
|
18
|
+
child = Random.new
|
19
|
+
composite = CompositeRng.new(parent, child, 42)
|
20
|
+
# ...
|
21
|
+
dice_roll = 1 + composite.rand(6)
|
22
|
+
# ...
|
23
|
+
```
|
24
|
+
The constructor takes three arguements:
|
25
|
+
* The parent PRNG that is used to "educate" the child.
|
26
|
+
* The child PRNG that is the actual generator of data.
|
27
|
+
* The churn factor that controls how much tutoring the child is to receive.
|
28
|
+
This optional parameter defaults to 16.
|
23
29
|
|
24
30
|
It is also possible to use the default PRNG as follows:
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
```ruby
|
32
|
+
parent = Object.new
|
33
|
+
child = Random.new
|
34
|
+
composite = CompositeRng.new(parent, child)
|
35
|
+
# ...
|
36
|
+
dice_roll = 1 + composite.rand(6)
|
37
|
+
# ...
|
38
|
+
```
|
33
39
|
This is because the default PRNG exists as methods of Object. Note that (as
|
34
40
|
far as I can tell) only one instance of that PRNG exists, so it should only
|
35
41
|
be used as parent or child but never both!.
|
36
42
|
|
37
|
-
|
38
|
-
|
39
|
-
|
43
|
+
The composite generator also works with custom generator, that support rand(n)
|
44
|
+
like my own Fibonacci generator:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
parent = Random.new
|
48
|
+
child = FibonacciRng.new(depth: 12)
|
49
|
+
composite = CompositeRng.new(parent, child)
|
50
|
+
# ...
|
51
|
+
dice_roll = 1 + composite.rand(6)
|
52
|
+
# ...
|
53
|
+
```
|
40
54
|
|
41
|
-
|
55
|
+
#### Accessing advanced methods with churn
|
56
|
+
|
57
|
+
Most PRNGs provide more resources beyond the basic "rand" method. Access to
|
58
|
+
these other methods is provided by using the churn method. The churn method
|
59
|
+
returns the child PRNG after it has received a suitable period of instruction
|
60
|
+
from the parent PRNG. An example, accessing the bytes method of the Random
|
61
|
+
class follows:
|
62
|
+
```ruby
|
63
|
+
# Get a string of 22 random chars.
|
64
|
+
crazy_string = composite.churn.bytes(22)
|
65
|
+
```
|
66
|
+
While this code seems to work, it really is weak tea. The issue is that the
|
67
|
+
education process is only carried out once. The following code does a much
|
68
|
+
more thorough job:
|
69
|
+
```ruby
|
70
|
+
# Get a string of 22 random chars.
|
71
|
+
crazy_string = 22.times.inject("") { |s| s << composite.churn.bytes(1) }
|
72
|
+
```
|
42
73
|
|
43
74
|
## Installation
|
44
75
|
|
@@ -54,10 +85,19 @@ Or install it yourself as:
|
|
54
85
|
|
55
86
|
$ gem install composite_rng
|
56
87
|
|
88
|
+
The composite_rng gem itself is found at: ( https://rubygems.org/gems/composite_rng )
|
89
|
+
|
57
90
|
## Contributing
|
58
91
|
|
59
|
-
|
92
|
+
#### Plan A
|
93
|
+
|
94
|
+
1. Fork it ( https://github.com/PeterCamilleri/composite_rng/fork )
|
60
95
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
96
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
97
|
4. Push to the branch (`git push origin my-new-feature`)
|
63
98
|
5. Create new Pull Request
|
99
|
+
|
100
|
+
#### Plan B
|
101
|
+
|
102
|
+
Go to the GitHub repository and raise an issue calling attention to some
|
103
|
+
aspect that could use some TLC or a suggestion or an idea.
|
data/irbt.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# An IRB + composite_rng Test bed
|
3
|
+
|
4
|
+
require 'irb'
|
5
|
+
$force_alias_read_line_module = true
|
6
|
+
require 'mini_readline'
|
7
|
+
|
8
|
+
puts "Starting an IRB console with composite_rng loaded."
|
9
|
+
|
10
|
+
if ARGV[0] == 'local'
|
11
|
+
require_relative 'lib/composite_rng'
|
12
|
+
puts "composite_rng loaded locally: #{CompositeRng::VERSION}"
|
13
|
+
|
14
|
+
ARGV.shift
|
15
|
+
else
|
16
|
+
require 'composite_rng'
|
17
|
+
puts "composite_rng loaded from gem: #{CompositeRng::VERSION}"
|
18
|
+
end
|
19
|
+
|
20
|
+
begin
|
21
|
+
require 'fibonacci_rng'
|
22
|
+
puts "fibonacci_rng loaded from gem: #{FibonacciRng::VERSION}"
|
23
|
+
rescue
|
24
|
+
puts "fibonacci_rng not loaded."
|
25
|
+
end
|
26
|
+
|
27
|
+
IRB.start
|
data/lib/composite_rng.rb
CHANGED
data/{Rakefile → rakefile.rb}
RENAMED
@@ -32,14 +32,7 @@ end
|
|
32
32
|
|
33
33
|
desc "Fire up an IRB session with composite_rng preloaded."
|
34
34
|
task :console do
|
35
|
-
|
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
|
35
|
+
system "ruby irbt.rb local"
|
43
36
|
end
|
44
37
|
|
45
38
|
desc "What version of the composite_rng is this?"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_rng
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Camilleri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -105,10 +105,11 @@ files:
|
|
105
105
|
- Gemfile
|
106
106
|
- LICENSE.txt
|
107
107
|
- README.md
|
108
|
-
- Rakefile
|
109
108
|
- composite_rng.gemspec
|
109
|
+
- irbt.rb
|
110
110
|
- lib/composite_rng.rb
|
111
111
|
- lib/composite_rng/version.rb
|
112
|
+
- rakefile.rb
|
112
113
|
- reek.txt
|
113
114
|
- tests/composite_rng_tests.rb
|
114
115
|
homepage: http://teuthida-technologies.com/
|