fibonacci_rng 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19f44541d4ae2cc3d474a5dd4d08d40a439911d6
4
- data.tar.gz: 1ca34b3ff58b59e0064c558107281dfb053cd289
3
+ metadata.gz: edf23cfb2d3c1fe1ce559fce4092779edb32ab88
4
+ data.tar.gz: 7df57182ab93e8c7256e9ace1624006a7a90c52a
5
5
  SHA512:
6
- metadata.gz: 20d6b5c324da8f31b804584f7862fe470cd669aaca2082c5f64c321a606e84bf528c76199d2b1f6980d80a0e60321b46489db613d7037332aaead28468d4c1da
7
- data.tar.gz: 07fc2949e518d780f3a938440354ab4c0f2d30ba0a3600c3b44c77b07d29d88f6480c024a27d5774bccb55da352d4b825082bb6e497e1d2e34418c5abff87cab
6
+ metadata.gz: 21a5a5033f1596615ed750d0aae39e8ea57bbe926d3f16e7b51909c2a232836a900ea544ce3707654e9e1369ef40e3c9001a5dacdb1dd63c0e6f2e800269e861
7
+ data.tar.gz: eb4b5154352662c05f5535b975e33bfa09b208f6b9cb1781f85e50dddcc1ebb53abacc99534b9a49a2170e30608cbbe3e3f12ba7e469470874dcc43074289d42
data/README.md CHANGED
@@ -53,6 +53,18 @@ and also available
53
53
  If more than one stream of numbers is required, it is best to use multiple
54
54
  instances of FibonacciRng objects rather than rely on one.
55
55
 
56
+ ### Hashing
57
+
58
+ As more as an experiment than anything else, it is also possible to use
59
+ the generator as a primitive hash generator. To do so, create a new
60
+ generator with a salt value, append data to it, and the retrieve the results
61
+ as a (big) number of or a string.
62
+
63
+ fib = FibonacciRng.new('salt')
64
+ fib << "The quick brown fox jumps over the lazy dog."
65
+ puts fib.hash_string
66
+ #displays: j5jqhk7ntrze02icv38gj28efa2qrctr6mi5ejbr2p4nj
67
+
56
68
  ## Contributing
57
69
 
58
70
  1. Fork it
data/Rakefile CHANGED
@@ -15,3 +15,10 @@ desc "Run a scan for smelly code!"
15
15
  task :reek do |t|
16
16
  `reek --no-color lib > reek.txt`
17
17
  end
18
+
19
+ desc "What version of fOOrth is this?"
20
+ task :vers do |t|
21
+ puts
22
+ puts "Fibonacci random number generator version = #{FibonacciRng::VERSION}"
23
+ end
24
+
@@ -2,5 +2,5 @@
2
2
 
3
3
  #The class of Fibonacci inspired random number generators.
4
4
  class FibonacciRng
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
data/lib/fibonacci_rng.rb CHANGED
@@ -43,6 +43,18 @@ class FibonacciRng
43
43
  srand(seed)
44
44
  end
45
45
 
46
+ #Get the value of the rng state as a number.
47
+ def hash_value
48
+ result = 0
49
+ @buffer[0...@depth].each {|value| result = (result << 29) + value }
50
+ result
51
+ end
52
+
53
+ #Get the value of the rng state as a string.
54
+ def hash_string
55
+ hash_value.to_s(36)
56
+ end
57
+
46
58
  #Set up a new seed value
47
59
  def srand(seed=FibonacciRng.new_seed)
48
60
  @seed = seed
@@ -107,6 +119,16 @@ class FibonacciRng
107
119
  end
108
120
  end
109
121
 
122
+ #Append data to the generator
123
+ def <<(data)
124
+ data.to_s.each_byte.each do |value|
125
+ index = @buffer[0] % @depth
126
+ do_spin
127
+ @buffer[index] += value
128
+ do_spin
129
+ end
130
+ end
131
+
110
132
  private
111
133
 
112
134
  #A class instance variable to hold the tickle value.
@@ -6,7 +6,7 @@ require 'minitest/autorun'
6
6
  require 'minitest_visible'
7
7
 
8
8
  #Test the monkey patches applied to the Object class.
9
- class CoreTester < Minitest::Test
9
+ class FibonacciRngTester < Minitest::Test
10
10
 
11
11
  #Track mini-test progress.
12
12
  include MinitestVisible
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+
3
+ require_relative '../lib/fibonacci_rng'
4
+ gem 'minitest'
5
+ require 'minitest/autorun'
6
+ require 'minitest_visible'
7
+
8
+ #Test the monkey patches applied to the Object class.
9
+ class FibonacciHasherTester < Minitest::Test
10
+
11
+ #Track mini-test progress.
12
+ include MinitestVisible
13
+
14
+ def test_that_it_can_be_read
15
+ fib = FibonacciRng.new('salt')
16
+
17
+ val = 6419946790796257801349293118951652810485853780437507149800008378866623
18
+ assert_equal(val, fib.hash_value)
19
+
20
+ str = 'le247zggdghf5vpcjbw2hrjf9eu8ql01wvl1fhv14bykf'
21
+ assert_equal(str, fib.hash_string)
22
+ end
23
+
24
+ def test_that_data_can_be_added_to_it
25
+ fib = FibonacciRng.new('salt')
26
+
27
+ fib << "The quick brown fox jumps over the lazy dog."
28
+
29
+ str = 'j5jqhk7ntrze02icv38gj28efa2qrctr6mi5ejbr2p4nj'
30
+ assert_equal(str, fib.hash_string)
31
+ end
32
+
33
+ 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.1.0
4
+ version: 0.2.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-06 00:00:00.000000000 Z
11
+ date: 2016-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest_visible
@@ -113,6 +113,7 @@ files:
113
113
  - prng.rb
114
114
  - reek.txt
115
115
  - tests/fibinacci_rng_tests.rb
116
+ - tests/hasher_tests.rb
116
117
  - tools/auto_corr.rb
117
118
  - tools/chi_squared.rb
118
119
  - tools/internal_rng.rb