fibonacci_rng 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Rakefile +16 -0
- data/fibonacci_rng.gemspec +4 -1
- data/lib/fibonacci_rng.rb +29 -13
- data/lib/fibonacci_rng/version.rb +4 -1
- data/reek.txt +2 -0
- data/tests/fibinacci_rng_tests.rb +81 -0
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MTc4YTlhNTQ0NDhjZWIxNDAyNzQzYjU1NDY0MzMzNWM0YzdkYmMyNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YWM4ODA4ZjQzZmNlYmYxYWQ0M2VmNDM5NzZlMDJkNzI3YmFmODA0MA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MTQ1ZjNmODYxNTc5ZDE0ZDc2YzNkZWY4ZGZmOTM2MzFjYTViNGE1NWI3ZWJl
|
10
|
+
NTM0NjViZTIzOWNhN2Q3MDI3NmNhODI0OTEwMDA2OTYyNjAyMTJhMTIxYTlm
|
11
|
+
MTY5ZTAyYzUwZjRjZWI3N2MxY2IyMDUwZmI3MDhjNWE4NTU5YWE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZmIyNjMzODVkMDNjNTZiNzk1ZTA4MTRhZDhlZTE1ZTcxMTZjYzZkMGVjZjQy
|
14
|
+
NDUwZjUxN2I3NGQ1Mjg4ZmVjMjQ2N2IzN2U0MTdmYzAzMWY1YzdjYmUxZjQ1
|
15
|
+
ODNlNTMyNDFkNjc2OTgyNjIxM2NmYzMwZTIyMDk4YmU4YzFmM2U=
|
data/Rakefile
CHANGED
@@ -1 +1,17 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# coding: utf-8
|
3
|
+
|
1
4
|
require "bundler/gem_tasks"
|
5
|
+
require 'rake/testtask'
|
6
|
+
|
7
|
+
#Run the fOOrth unit test suite.
|
8
|
+
Rake::TestTask.new do |t|
|
9
|
+
#List out all the test files.
|
10
|
+
t.test_files = FileList['tests/**/*.rb']
|
11
|
+
t.verbose = false
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Run a scan for smelly code!"
|
15
|
+
task :reek do |t|
|
16
|
+
`reek --no-color lib > reek.txt`
|
17
|
+
end
|
data/fibonacci_rng.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = FibonacciRng::VERSION
|
9
9
|
spec.authors = ["Peter Camilleri"]
|
10
10
|
spec.email = ["peter.c.camilleri@gmail.com"]
|
11
|
-
spec.description = "A Fibonacci inspired pseudo random number generator. Just try and prove that
|
11
|
+
spec.description = "A Fibonacci inspired pseudo random number generator. Just try and prove that it sucks! ;-)"
|
12
12
|
spec.summary = "A Fibonacci inspired pseudo random number generator."
|
13
13
|
spec.homepage = ""
|
14
14
|
spec.license = "MIT"
|
@@ -20,4 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
22
|
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency 'reek', "~> 1.3.8"
|
24
|
+
spec.add_development_dependency 'minitest', "~> 4.7.5"
|
25
|
+
|
23
26
|
end
|
data/lib/fibonacci_rng.rb
CHANGED
@@ -1,6 +1,18 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
1
3
|
require "fibonacci_rng/version"
|
2
4
|
|
5
|
+
#The class of Fibonacci inspired random number generators.
|
3
6
|
class FibonacciRng
|
7
|
+
|
8
|
+
#A class instance variable to hold the tickle value.
|
9
|
+
@tickle = '0'
|
10
|
+
|
11
|
+
#Create the default seed string.
|
12
|
+
def self.seed
|
13
|
+
Time.now.to_s + @tickle.succ!
|
14
|
+
end
|
15
|
+
|
4
16
|
CHOP = 0x3FFFFFFF
|
5
17
|
BYTE = 0xFF
|
6
18
|
WORD = 0xFFFF
|
@@ -10,22 +22,18 @@ class FibonacciRng
|
|
10
22
|
attr_reader :depth
|
11
23
|
|
12
24
|
#Initialize the PRN generator
|
13
|
-
def initialize(depth=8, seed=
|
25
|
+
def initialize(depth=8, seed=FibonacciRng.seed)
|
14
26
|
fail "Invalid depth value (3..30)." unless (3..30) === depth
|
15
27
|
@depth = depth
|
16
28
|
reseed(seed)
|
17
29
|
end
|
18
30
|
|
19
31
|
#Set up a new seed value
|
20
|
-
def reseed(seed=
|
32
|
+
def reseed(seed=FibonacciRng.seed)
|
21
33
|
@buffer = Array.new(@depth+2, 0)
|
22
34
|
seedsrc = (seed.to_s + seed.class.to_s + 'Leonardo Pisano').each_byte.cycle
|
23
35
|
indxsrc = (0...depth).cycle
|
24
|
-
|
25
|
-
1024.times do
|
26
|
-
@buffer[indxsrc.next] += seedsrc.next
|
27
|
-
do_spin
|
28
|
-
end
|
36
|
+
do_reseed(indxsrc, seedsrc)
|
29
37
|
end
|
30
38
|
|
31
39
|
#Roll a dice.
|
@@ -41,13 +49,13 @@ class FibonacciRng
|
|
41
49
|
end
|
42
50
|
|
43
51
|
#Get a pseudo random word
|
44
|
-
def
|
52
|
+
def word
|
45
53
|
do_spin
|
46
54
|
@buffer[0] & WORD
|
47
55
|
end
|
48
56
|
|
49
57
|
#Get a pseudo random float
|
50
|
-
def
|
58
|
+
def float
|
51
59
|
do_spin
|
52
60
|
@buffer[0].to_f / BASE
|
53
61
|
end
|
@@ -60,13 +68,21 @@ class FibonacciRng
|
|
60
68
|
end
|
61
69
|
|
62
70
|
private
|
71
|
+
#Do the work of reseeding the PRNG
|
72
|
+
def do_reseed(indxsrc, seedsrc)
|
73
|
+
1024.times do
|
74
|
+
@buffer[indxsrc.next] += seedsrc.next
|
75
|
+
do_spin
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
63
79
|
#Cycle through the PRNG once.
|
64
80
|
def do_spin
|
65
|
-
buffer[-2] = buffer[0]
|
66
|
-
buffer[-1] = buffer[1]
|
81
|
+
@buffer[-2] = @buffer[0]
|
82
|
+
@buffer[-1] = @buffer[1]
|
67
83
|
|
68
|
-
0...depth do |
|
69
|
-
buffer[
|
84
|
+
0...depth do |idx|
|
85
|
+
@buffer[idx] = (@buffer[idx+1] + (@buffer[idx+2] >> 1)) & CHOP
|
70
86
|
end
|
71
87
|
end
|
72
88
|
|
data/reek.txt
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../lib/fibonacci_rng'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
#Test the monkey patches applied to the Object class.
|
7
|
+
class CoreTester < 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 = FibonacciRng.new
|
24
|
+
|
25
|
+
100.times do
|
26
|
+
assert((0...6) === prng.dice(6))
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_that_it_creates_bytes
|
32
|
+
prng = FibonacciRng.new
|
33
|
+
|
34
|
+
100.times do
|
35
|
+
assert((0...256) === prng.byte)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_that_it_creates_words
|
41
|
+
prng = FibonacciRng.new
|
42
|
+
|
43
|
+
100.times do
|
44
|
+
assert((0...65536) === prng.word)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_that_it_makes_unique_sequnces
|
50
|
+
prnga = FibonacciRng.new
|
51
|
+
prngb = FibonacciRng.new
|
52
|
+
|
53
|
+
buffa = []
|
54
|
+
buffb = []
|
55
|
+
|
56
|
+
100.times do
|
57
|
+
buffa << prnga.dice(6)
|
58
|
+
buffb << prngb.dice(6)
|
59
|
+
end
|
60
|
+
|
61
|
+
assert(buffa != buffb)
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_that_it_makes_repeatable_sequnces
|
66
|
+
prnga = FibonacciRng.new(8, 0)
|
67
|
+
prngb = FibonacciRng.new(8, 0)
|
68
|
+
|
69
|
+
buffa = []
|
70
|
+
buffb = []
|
71
|
+
|
72
|
+
100.times do
|
73
|
+
buffa << prnga.dice(6)
|
74
|
+
buffb << prngb.dice(6)
|
75
|
+
end
|
76
|
+
|
77
|
+
assert(buffa == buffb)
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fibonacci_rng
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Camilleri
|
@@ -38,8 +38,36 @@ dependencies:
|
|
38
38
|
- - ! '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
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
|
41
69
|
description: A Fibonacci inspired pseudo random number generator. Just try and prove
|
42
|
-
that
|
70
|
+
that it sucks! ;-)
|
43
71
|
email:
|
44
72
|
- peter.c.camilleri@gmail.com
|
45
73
|
executables: []
|
@@ -54,6 +82,8 @@ files:
|
|
54
82
|
- fibonacci_rng.gemspec
|
55
83
|
- lib/fibonacci_rng.rb
|
56
84
|
- lib/fibonacci_rng/version.rb
|
85
|
+
- reek.txt
|
86
|
+
- tests/fibinacci_rng_tests.rb
|
57
87
|
homepage: ''
|
58
88
|
licenses:
|
59
89
|
- MIT
|