fibonacci_rng 0.2.0 → 0.3.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 +53 -22
- data/lib/fibonacci_rng.rb +6 -3
- data/lib/fibonacci_rng/version.rb +1 -1
- data/{Rakefile → rakefile.rb} +2 -2
- data/tests/fibinacci_rng_tests.rb +17 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c8a1e946cc05b8a0f7ef15c0f9f332f7170c1a8
|
4
|
+
data.tar.gz: 69703e8ba9a17e3abb5b1cfbd67a91da806e8f7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b285d3413bb9e669ca12030af0050672de993c0d65cf188cd25102a02d07b5df08c3091d7bf7d2f216d4d0a2b520243c0172494d62109c6ddeb738d0cda27da
|
7
|
+
data.tar.gz: 07d897637e993f0bb05088f8e336851e9f51177326dac0922de78b1bf63a09cef4e6af9cfe4c612a8dad4367b911e660f97a91340a5bb067f56097471792f9c7
|
data/README.md
CHANGED
@@ -23,47 +23,78 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
-
|
26
|
+
```ruby
|
27
|
+
require 'fibonacci_rng'
|
28
|
+
```
|
27
29
|
|
28
30
|
Then in an appropriate place in the code:
|
29
31
|
|
30
|
-
|
32
|
+
```ruby
|
33
|
+
@my_rng = FibonacciRng.new(depth, seed_value)
|
34
|
+
```
|
31
35
|
|
32
|
-
Where depth is an optional integer value between
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
+
Where depth is an optional integer value between 2 and 99 and the seed value
|
37
|
+
is a number or string or other object that has a repeatable value. You can
|
38
|
+
also get a "random" generator of the default depth (8) and a randomized
|
39
|
+
seed. Here is an overview of the available options.
|
36
40
|
|
37
|
-
|
41
|
+
```ruby
|
42
|
+
@my_rng = FibonacciRng.new # Random seed, depth = 8
|
43
|
+
@my_rng = FibonacciRng.new(seed) # Specified seed, depth = 8
|
44
|
+
@my_rng = FibonacciRng.new(seed, 12) # Specified seed, depth = 12
|
45
|
+
@my_rng = FibonacciRng.new(FibonacciRng.new_seed, 12) # Random seed, depth = 12
|
46
|
+
|
47
|
+
```
|
38
48
|
|
39
49
|
To get some data out here are some options:
|
40
50
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
51
|
+
```ruby
|
52
|
+
@my_rng.dice(100) # A "random" integer between 0 and 99
|
53
|
+
@my_rng.byte # A "random" integer between 0 and 255
|
54
|
+
@my_rng.word # A "random" integer between 0 and 65535
|
55
|
+
@my_rng.float # A "random" float between 0 and less than 1.
|
56
|
+
```
|
45
57
|
|
46
58
|
and also available
|
47
59
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
60
|
+
```ruby
|
61
|
+
@my_rng.reseed(value) # Reseed the sequence with the new value.
|
62
|
+
@my_rng.reseed # Reseed the sequence with a "random" seed.
|
63
|
+
@my_rng.spin(count) # Spin the generator count times.
|
64
|
+
@my_rng.spin # Spin the generator once.
|
65
|
+
```
|
52
66
|
|
53
67
|
If more than one stream of numbers is required, it is best to use multiple
|
54
|
-
instances of FibonacciRng objects rather than rely on one.
|
68
|
+
instances of FibonacciRng objects rather than rely on one. This will help avoid
|
69
|
+
the two streams of data being correlated.
|
55
70
|
|
56
71
|
### Hashing
|
57
72
|
|
58
73
|
As more as an experiment than anything else, it is also possible to use
|
59
74
|
the generator as a primitive hash generator. To do so, create a new
|
60
75
|
generator with a salt value, append data to it, and the retrieve the results
|
61
|
-
as a (big) number
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
76
|
+
as a (big) number or a string.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
fib = FibonacciRng.new('salt')
|
80
|
+
fib << "The quick brown fox jumps over the lazy dog."
|
81
|
+
puts fib.hash_string
|
82
|
+
#displays: j5jqhk7ntrze02icv38gj28efa2qrctr6mi5ejbr2p4nj
|
83
|
+
```
|
84
|
+
Note that the length of the hash string is a function of the depth of the
|
85
|
+
generator used to create it. This is about 5.5 characters per unit of depth.
|
86
|
+
|
87
|
+
### Salting
|
88
|
+
|
89
|
+
Another (more practical) use for the Fibonacci generator is the creation of
|
90
|
+
salting strings for use in more capable hashing schemes. Here are some possible
|
91
|
+
ways that this can be done:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
salt_string = FibonacciRng.new.hash_string
|
95
|
+
salt_string = FibonacciRng.new(FibonacciRng.new_seed, 12).hash_string()
|
96
|
+
```
|
97
|
+
Each time any of these is run, a different salt string will be generated.
|
67
98
|
|
68
99
|
## Contributing
|
69
100
|
|
data/lib/fibonacci_rng.rb
CHANGED
@@ -38,7 +38,7 @@ class FibonacciRng
|
|
38
38
|
|
39
39
|
#Initialize the PRN generator
|
40
40
|
def initialize(seed=FibonacciRng.new_seed, depth=8)
|
41
|
-
fail "Invalid depth value (
|
41
|
+
fail "Invalid depth value (2..99)." unless (2..99) === depth
|
42
42
|
@depth = depth
|
43
43
|
srand(seed)
|
44
44
|
end
|
@@ -134,14 +134,17 @@ class FibonacciRng
|
|
134
134
|
#A class instance variable to hold the tickle value.
|
135
135
|
@tickle = '0'
|
136
136
|
|
137
|
+
#A synchronizer for access to the @tickle variable
|
138
|
+
@sync = Mutex.new
|
139
|
+
|
137
140
|
#Create the default seed string.
|
138
141
|
def self.new_seed
|
139
|
-
Time.now.to_s + @tickle.succ!
|
142
|
+
@sync.synchronize {Time.now.to_s + @tickle.succ!}
|
140
143
|
end
|
141
144
|
|
142
145
|
#Do the work of reseeding the PRNG
|
143
146
|
def do_reseed(indxsrc, seedsrc)
|
144
|
-
|
147
|
+
(128*@depth).times do
|
145
148
|
@buffer[indxsrc.next] += seedsrc.next
|
146
149
|
do_spin
|
147
150
|
end
|
data/{Rakefile → rakefile.rb}
RENAMED
@@ -4,7 +4,7 @@
|
|
4
4
|
require "bundler/gem_tasks"
|
5
5
|
require 'rake/testtask'
|
6
6
|
|
7
|
-
#Run the
|
7
|
+
#Run the Fibonacci unit test suite.
|
8
8
|
Rake::TestTask.new do |t|
|
9
9
|
#List out all the test files.
|
10
10
|
t.test_files = FileList['tests/**/*.rb']
|
@@ -16,7 +16,7 @@ task :reek do |t|
|
|
16
16
|
`reek --no-color lib > reek.txt`
|
17
17
|
end
|
18
18
|
|
19
|
-
desc "What version of
|
19
|
+
desc "What version of the Fibonacci is this?"
|
20
20
|
task :vers do |t|
|
21
21
|
puts
|
22
22
|
puts "Fibonacci random number generator version = #{FibonacciRng::VERSION}"
|
@@ -11,6 +11,12 @@ class FibonacciRngTester < Minitest::Test
|
|
11
11
|
#Track mini-test progress.
|
12
12
|
include MinitestVisible
|
13
13
|
|
14
|
+
|
15
|
+
def test_that_rejects_bad_parms
|
16
|
+
assert_raises { FibonacciRng.new('seed', 1) }
|
17
|
+
assert_raises { FibonacciRng.new('seed', 65536) }
|
18
|
+
end
|
19
|
+
|
14
20
|
def test_that_it_creates_dice_rolls
|
15
21
|
prng = FibonacciRng.new
|
16
22
|
|
@@ -101,4 +107,15 @@ class FibonacciRngTester < Minitest::Test
|
|
101
107
|
|
102
108
|
assert(buffa == buffb)
|
103
109
|
end
|
110
|
+
|
111
|
+
def test_that_it_creates_unique_seeds
|
112
|
+
result = []
|
113
|
+
10_000.times do
|
114
|
+
result << FibonacciRng.new_seed
|
115
|
+
end
|
116
|
+
|
117
|
+
result.uniq!
|
118
|
+
assert_equal(10_000, result.length)
|
119
|
+
end
|
120
|
+
|
104
121
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fibonacci_rng
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.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-03-
|
11
|
+
date: 2016-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest_visible
|
@@ -106,11 +106,11 @@ files:
|
|
106
106
|
- Gemfile
|
107
107
|
- LICENSE.txt
|
108
108
|
- README.md
|
109
|
-
- Rakefile
|
110
109
|
- fibonacci_rng.gemspec
|
111
110
|
- lib/fibonacci_rng.rb
|
112
111
|
- lib/fibonacci_rng/version.rb
|
113
112
|
- prng.rb
|
113
|
+
- rakefile.rb
|
114
114
|
- reek.txt
|
115
115
|
- tests/fibinacci_rng_tests.rb
|
116
116
|
- tests/hasher_tests.rb
|