composite_rng 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -11
- data/lib/composite_rng.rb +26 -4
- data/lib/composite_rng/version.rb +1 -1
- data/tests/composite_rng_tests.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f340e108ce670dca8fd91ec9089ae5753eae06e
|
4
|
+
data.tar.gz: b992ad5f3defe4e2578090833730107b256bfc89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1731dee7aeb11ec2f9006853c038211c89afcaa4a5e4e9f4e6b3f74e24339218d39c2108e270feea0b05aa01a9f6277d365fb27b59e1397d2799f06d314f828d
|
7
|
+
data.tar.gz: cf12a60cc2dba80842ef8f2e73dede341cb0a7e6766f1800a6bec6f16b00fbf3a2dd030f7e380818288f3670120f1d12a18d824985ea45dce084da9176f947a4
|
data/README.md
CHANGED
@@ -2,30 +2,36 @@
|
|
2
2
|
|
3
3
|
The composite (psuedo) random number generator is a container for two other
|
4
4
|
(psuedo) random number generators. By working together, these create higher
|
5
|
-
quality random number streams than either could by itself.
|
5
|
+
quality pseudo-random number streams than either could by itself.
|
6
6
|
|
7
|
-
The two generators do
|
8
|
-
|
9
|
-
|
7
|
+
The two generators do not need to be the same type. In fact it helps to reduce
|
8
|
+
any possible correlation between the parent and the child if they are
|
9
|
+
completely different sorts of generators. It is really not a good idea for them
|
10
|
+
to be either the same instance, or the same type with the same seed, because
|
11
|
+
this negates the advantage of compositing generators.
|
10
12
|
|
11
13
|
The generators used must comply to the following duck characteristics:
|
12
14
|
|
13
15
|
rand(max) - This method must compute a random integer from 0...max
|
14
16
|
|
15
|
-
|
17
|
+
The constructor takes four arguments:
|
18
|
+
* The parent PRNG that is used to "educate" the child.
|
19
|
+
* The child PRNG that is the actual generator of data.
|
20
|
+
* The optional churn_limit factor that controls how much tutoring the child
|
21
|
+
can receive. This optional parameter defaults to 16. This value may be read
|
22
|
+
back with the churn_limit property. Valid values are 2..256
|
23
|
+
* The optional init factor that controls the amount of initial tutoring the
|
24
|
+
child receives initially. This defaults to 0 for none. Valid values are 0..256
|
25
|
+
|
26
|
+
The following shows how this could work:
|
16
27
|
```ruby
|
17
28
|
parent = Random.new #Get the built in Mersenne Twister MT19937 PRNG
|
18
29
|
child = Random.new
|
19
|
-
composite = CompositeRng.new(parent, child, 42)
|
30
|
+
composite = CompositeRng.new(parent, child, 42, 11)
|
20
31
|
# ...
|
21
32
|
dice_roll = 1 + composite.rand(6)
|
22
33
|
# ...
|
23
34
|
```
|
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.
|
29
35
|
|
30
36
|
It is also possible to use the default PRNG as follows:
|
31
37
|
```ruby
|
@@ -87,6 +93,8 @@ Or install it yourself as:
|
|
87
93
|
|
88
94
|
The composite_rng gem itself is found at: ( https://rubygems.org/gems/composite_rng )
|
89
95
|
|
96
|
+
The fibonacci_rng code lives at: ( https://github.com/PeterCamilleri/fibonacci_rng )
|
97
|
+
|
90
98
|
## Contributing
|
91
99
|
|
92
100
|
#### Plan A
|
data/lib/composite_rng.rb
CHANGED
@@ -3,13 +3,35 @@ require_relative "composite_rng/version"
|
|
3
3
|
#A class of random number generators that work by nesting other PRNGs.
|
4
4
|
class CompositeRng
|
5
5
|
|
6
|
+
#The limits on the churn limit.
|
7
|
+
CHURNS = 2..256
|
8
|
+
|
9
|
+
#The limits on the init.
|
10
|
+
INITS = 0..256
|
11
|
+
|
12
|
+
#The current upper limit on churn
|
13
|
+
attr_reader :churn_limit
|
14
|
+
|
6
15
|
#Create a composite PRNG
|
7
16
|
#<br>Parameters
|
8
17
|
#* parent - the starting PRNG
|
9
18
|
#* child - the progeny PRNG
|
10
|
-
#*
|
11
|
-
|
12
|
-
|
19
|
+
#* churn_limit - the max amount of churning done.
|
20
|
+
#* init = the number of initial churn steps.
|
21
|
+
def initialize(parent, child, churn_limit=16, init=0)
|
22
|
+
@parent, @child, @churn_limit = parent, child, churn_limit
|
23
|
+
|
24
|
+
#Validate the churn_limit
|
25
|
+
unless CHURNS === @churn_limit
|
26
|
+
fail "Invalid churn limit #{@churn_limit}. Allowed values are #{CHURNS}"
|
27
|
+
end
|
28
|
+
|
29
|
+
#Validate the init
|
30
|
+
unless INITS === init
|
31
|
+
fail "Invalid init value #{init}. Allowed values are #{INITS}"
|
32
|
+
end
|
33
|
+
|
34
|
+
init.times { churn }
|
13
35
|
end
|
14
36
|
|
15
37
|
#An access point for random numbers.
|
@@ -25,7 +47,7 @@ class CompositeRng
|
|
25
47
|
#<br>Returns
|
26
48
|
#* a churned random number generator.
|
27
49
|
def churn
|
28
|
-
(1 + @parent.rand(@
|
50
|
+
(1 + @parent.rand(@churn_limit)).times {@child.rand(256)}
|
29
51
|
@child
|
30
52
|
end
|
31
53
|
|
@@ -11,6 +11,14 @@ class CompositeRngTester < Minitest::Test
|
|
11
11
|
#Track mini-test progress.
|
12
12
|
include MinitestVisible
|
13
13
|
|
14
|
+
def test_that_it_checks_parms
|
15
|
+
assert_raises { CompositeRng.new(Random.new, Random.new, -1, 0) }
|
16
|
+
assert_raises { CompositeRng.new(Random.new, Random.new, 300, 0) }
|
17
|
+
|
18
|
+
assert_raises { CompositeRng.new(Random.new, Random.new, 2, -1) }
|
19
|
+
assert_raises { CompositeRng.new(Random.new, Random.new, 2, 300) }
|
20
|
+
end
|
21
|
+
|
14
22
|
def test_that_it_creates_dice_rolls
|
15
23
|
prng = CompositeRng.new(Random.new, Random.new)
|
16
24
|
|
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.1.0
|
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-05-
|
11
|
+
date: 2016-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|