fibonacci_rng 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTI1ZTdjMjg4MTMyN2Q0YjBiNjg4NzViYThlZGExOGM4ZmFkYjc4MQ==
4
+ NTJlYjNlYzYxMGMxMzAyOTk2MmEyYWM2OWI5N2RlN2JhMWQ5ZDQ3OA==
5
5
  data.tar.gz: !binary |-
6
- MGM3NTk5MDk0MDM5ZWE1MGZiYmY3NTM3NjAxZDRiZGExMWFlZjI1OA==
6
+ OGQxYWNhZjViOTA5MjYzNDI0OTNkZTgxMTllNmYyOWRkNmQ5MDUwMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTdmMTkzNzQ5OGU2YThkNzVlN2NmYjJkYjU5YjBjZTE2NTg2M2FmOTllOWI5
10
- MjdlMzZlY2UwODMwODkxYjYyNWNlZTZhNTkwODYzNWI3ZDA0OTM2ZDgyNWVm
11
- Y2VkYzA0YzQ3MzFlZTg2NDRjZmIyNWE4NmFlZmU0NmFiMzFiYWY=
9
+ ZTlhNWYzYzRjOTQ0OTljMjE5ZGM4NjA1NTZlNDkxNTc4ZDNjYmE3NGJiM2Yy
10
+ YzFjZTA2YzM1ZWJhZDdiMWNlZWIxY2YyYWU1ODNiNjY4ZWVmNWZhOTQyNmE2
11
+ NjI1N2RjMzZlNWY1NThkMWZjZTJhMTRjMDgyN2QwMjhhYmQxZmY=
12
12
  data.tar.gz: !binary |-
13
- YWQwOGZkZWMzNzZjNWY0MGVhNDE1Mzk2ZmZhNzM5MDlkZGNmYjY0MmY2ODNi
14
- NDhhNDNjNDQ1NjMxYzRiMTVkZGM5ZmU0YWRmYTk1YmQzNzkwYjMyZDNmODlm
15
- MTFiOWJkYjE4YzJmOTFkMGU1OGZkM2Y5YTljMDZhMDMyMGY1NDc=
13
+ ZmU1MGRmYzE4OTk2MTUyOGUzNTExNjMwNzdjYWFkM2ZkYTIxNmZlZmQxOGJm
14
+ MTkxY2NjYzIyNzYxNzBjN2M5NzMwMmM5YjY5M2FmMDVhNjZjNGU5NDMzOTVm
15
+ MDM4ZWIwNThhNzJhN2NhNGViYzQ1MjY4NzUwMThmYmM1NjdlZGM=
@@ -2,5 +2,5 @@
2
2
 
3
3
  #The class of Fibonacci inspired random number generators.
4
4
  class FibonacciRng
5
- VERSION = "0.0.3"
5
+ VERSION = "0.0.4"
6
6
  end
data/lib/fibonacci_rng.rb CHANGED
@@ -13,10 +13,10 @@ class FibonacciRng
13
13
  Time.now.to_s + @tickle.succ!
14
14
  end
15
15
 
16
- CHOP = 0x3FFFFFFF
16
+ CHOP = 0x1FFFFFFF
17
17
  BYTE = 0xFF
18
18
  WORD = 0xFFFF
19
- BASE = 1073741824.0
19
+ BASE = (CHOP+1).to_f
20
20
 
21
21
  #An accessor for the depth!
22
22
  attr_reader :depth
@@ -38,7 +38,12 @@ class FibonacciRng
38
38
 
39
39
  #Roll a dice.
40
40
  def dice(sides)
41
- do_spin
41
+ limit = ((CHOP+1) / sides) * sides
42
+
43
+ begin
44
+ do_spin
45
+ end until @buffer[0] < limit
46
+
42
47
  @buffer[0] % sides
43
48
  end
44
49
 
data/prng.rb ADDED
@@ -0,0 +1,79 @@
1
+ # coding: utf-8
2
+
3
+ #The Pseudo Random Number Generation Test Suite.
4
+ module PrngTestSuite
5
+
6
+ #Display usage info and exit
7
+ def self.usage(msg=nil)
8
+ puts
9
+ puts "PrngTestSuite Version 0.0.2"
10
+
11
+ if msg
12
+ puts
13
+ puts msg
14
+ end
15
+
16
+ puts
17
+ puts "Usage Info:"
18
+ puts
19
+ puts "$ ruby prng.rb <locn> <gen> <test> <max> <count>"
20
+ puts
21
+ puts "Where:"
22
+ puts " <locn> fib code location, either 'gem' or 'local'"
23
+ puts " <gen> select a generator, either 'fib' or 'int'"
24
+ puts " <test> select a test, either 'chisq' or 'auto'"
25
+ puts " <max> the number of bins to test (2..65535)"
26
+ puts " <count> the number of samples to test (1..1000000)"
27
+ puts
28
+
29
+ exit
30
+ end
31
+
32
+ end
33
+
34
+ if ARGV[0] == 'gem'
35
+ require 'fibonacci_rng'
36
+ elsif ARGV[0] == 'local'
37
+ require_relative 'lib/fibonacci_rng'
38
+ elsif ARGV[0] == 'help'
39
+ PrngTestSuite.usage
40
+ else
41
+ PrngTestSuite.usage "Error: missing or invalid locn parameter."
42
+ end
43
+
44
+ require_relative 'tools/internal_rng'
45
+ require_relative 'tools/chi_squared'
46
+ require_relative 'tools/auto_corr'
47
+
48
+ module PrngTestSuite
49
+
50
+ def self.main
51
+ if ARGV[1] == 'fib'
52
+ gen = FibonacciRng.new
53
+ elsif ARGV[1] == 'int'
54
+ gen = InternalRng.new
55
+ else
56
+ PrngTestSuite.usage "Error: missing or invalid gen parameter."
57
+ end
58
+
59
+ if ARGV[2] == 'chisq'
60
+ tester = ChiSquaredTest.new
61
+ elsif ARGV[2] == 'auto'
62
+ tester = AutoCorrelator.new
63
+ else
64
+ PrngTestSuite.usage "Error: missing or invalid test parameter."
65
+ end
66
+
67
+ unless (2..65535) === (max = ARGV[3].to_i)
68
+ PrngTestSuite.usage "Error: missing or invalid max parameter."
69
+ end
70
+
71
+ unless (1..10000000) === (count = ARGV[4].to_i)
72
+ PrngTestSuite.usage "Error: missing or invalid count parameter."
73
+ end
74
+
75
+ tester.run(gen, max, count)
76
+ end
77
+ end
78
+
79
+ PrngTestSuite.main
@@ -44,6 +44,16 @@ class CoreTester < MiniTest::Unit::TestCase
44
44
 
45
45
  end
46
46
 
47
+ def test_that_it_creates_floats
48
+ prng = FibonacciRng.new
49
+
50
+ 100.times do
51
+ value = prng.float
52
+ assert((value >= 0.0) && (value < 1.0))
53
+ end
54
+
55
+ end
56
+
47
57
  def test_that_it_makes_unique_sequnces
48
58
  prnga = FibonacciRng.new
49
59
  prngb = FibonacciRng.new
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+
3
+ class AutoCorrelator
4
+
5
+ #Run a auto correlation test on the random number generator.
6
+ def run(gen, _width, count)
7
+ data = Array.new(2*count) { gen.float - 0.5}
8
+ result = Array.new(count)
9
+
10
+ (0..count).each do |lag|
11
+ result[lag] = correlate(data, count, lag)
12
+ end
13
+
14
+ puts result
15
+
16
+ end
17
+
18
+ #Perform a single correlation
19
+ def correlate(data, count, lag)
20
+ result = 0.0
21
+
22
+ (0...count).each do |idx|
23
+ result += data[idx] * data[idx+lag]
24
+ end
25
+
26
+ result
27
+ end
28
+
29
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+
3
+ #A Chi Squared tester
4
+ class ChiSquaredTest
5
+
6
+ #Run a chi squared test on the random number generator.
7
+ def run(gen, bin_size, count)
8
+ max = bin_size
9
+ bins = Array.new(bin_size, 0)
10
+
11
+ start_time = Time.now
12
+ puts "Starting test"
13
+
14
+ count.times do
15
+ bins[gen.dice(max)] += 1
16
+ end
17
+
18
+ puts "Test completed (#{Time.now - start_time} elapsed)"
19
+ puts "Raw Bins = #{bins.inspect}"
20
+
21
+ normalized = bins.collect {|value| value.to_f/count }
22
+
23
+ #puts "Nrm Bins = #{normalized.inspect}"
24
+
25
+ expected = 1/max.to_f
26
+ #puts "Expected = #{expected}"
27
+
28
+ err_sqr = normalized.collect{|value| diff = expected - value; diff*diff}
29
+ #puts "Esq Bins = #{err_sqr.inspect}"
30
+
31
+ chi_sq = err_sqr.inject {|sum, value| sum + value}
32
+ puts "Chi Squared = #{chi_sq}"
33
+ end
34
+
35
+ end
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+
3
+ #A wrapper for the internal random number generator
4
+ class InternalRng
5
+
6
+ #Roll the dice!
7
+ def dice(sides)
8
+ rand(sides)
9
+ end
10
+
11
+ #Get a float!
12
+ def float
13
+ rand(0)
14
+ end
15
+
16
+ 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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Camilleri
@@ -82,8 +82,12 @@ files:
82
82
  - fibonacci_rng.gemspec
83
83
  - lib/fibonacci_rng.rb
84
84
  - lib/fibonacci_rng/version.rb
85
+ - prng.rb
85
86
  - reek.txt
86
87
  - tests/fibinacci_rng_tests.rb
88
+ - tools/auto_corr.rb
89
+ - tools/chi_squared.rb
90
+ - tools/internal_rng.rb
87
91
  homepage: ''
88
92
  licenses:
89
93
  - MIT