fibonacci_rng 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MGYzYmYyMDAxMTMwNTczYmI0MmMwZjc5OTk1YTJmZTg1YTJhZTlhYQ==
5
+ data.tar.gz: !binary |-
6
+ YTU5ZTUxYjMxNmFiN2RhOTZmYTdjYWY1OWQ5YWQyOWVkMTk4NTBjOQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OTE5OTkzZGE3M2I3MzI3NjU0NjExMjlmNDNkYTBmZWZlNDJiMmNmNzM1NWZk
10
+ NzNiNWE3NTI3OTY3ZGJlZDAyYTFhNDUzMWJjMjRjMjFjZmFmNGM4MGMzOWI2
11
+ ZjVhMTk5OGVmMWYzYTUyZjYzNjEwNzMwMzI3ZWQ0YmM1ZDUxMmI=
12
+ data.tar.gz: !binary |-
13
+ ZTFhMWVlYjczNDkxNGE5YzhhNzQ2ZDliYmY0Y2VmOTAyZjc0OGVmNGFkN2Rh
14
+ YzlhMTU0YWZiNGZmNzJlZWI2MWY2MmViNzM2OWM2ZTAzYTVkYzNiY2I1MDRm
15
+ NjU5ZGVkMTdiNGNhOTY0ZWFiYzVmOGU2NzAyNmM0YWEyMzFlN2I=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fibonacci_rng.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Peter Camilleri
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # FibonacciRng
2
+
3
+ This gem implements a random number generator inspired by the famous Fibonacci
4
+ number sequence. This is meant to serve as an example of a lesser or poorly
5
+ implemented random number generator. Until now, the linear congruential and
6
+ middle squares generators have been picked on as poor, so I felt that a new
7
+ contender was required. Proving that this is indeed a poor RNG is left as an
8
+ exercise for the reader!
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'fibonacci_rng'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install fibonacci_rng
23
+
24
+ ## Usage
25
+
26
+ require 'fibonacci_rng'
27
+
28
+ Then in an appropriate place in the code:
29
+
30
+ @my_rng = FibonacciRng.new(depth, seed_value)
31
+
32
+ Where depth is an optional integer value between 3 and are you kidding and
33
+ the seed value is a number or string or other object that has a repeatable
34
+ value. To get some data out here are some options:
35
+
36
+ @my_rng.dice(100) # A "random" integer between 0 and 99
37
+ @my_rng.byte # A "random" integer between 0 and 255
38
+ @my_rng.word # A "random" integer between 0 and 65535
39
+ @my_rng.float # A "random" float between 0 and less than 1.
40
+
41
+ and also available
42
+
43
+ @my_rng.reseed(value) # Reseed the sequence with the new value.
44
+ @my_rng.reseed # Reseed the sequence with a "random" seed.
45
+ @my_rng.spin(count) # Spin the generator count times.
46
+ @my_rng.spin # Spin the generator once.
47
+
48
+ If more than one stream of numbers is required, it is best to use multiple
49
+ instances of FibonacciRng objects rather than rely on one.
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fibonacci_rng/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fibonacci_rng"
8
+ spec.version = FibonacciRng::VERSION
9
+ spec.authors = ["Peter Camilleri"]
10
+ spec.email = ["peter.c.camilleri@gmail.com"]
11
+ spec.description = "A Fibonacci inspired pseudo random number generator. Just try and prove that is sucks! ;-)"
12
+ spec.summary = "A Fibonacci inspired pseudo random number generator."
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,73 @@
1
+ require "fibonacci_rng/version"
2
+
3
+ class FibonacciRng
4
+ CHOP = 0x3FFFFFFF
5
+ BYTE = 0xFF
6
+ WORD = 0xFFFF
7
+ BASE = 1073741824.0
8
+
9
+ #An accessor for the depth!
10
+ attr_reader :depth
11
+
12
+ #Initialize the PRN generator
13
+ def initialize(depth=8, seed=Time.now.to_s)
14
+ fail "Invalid depth value (3..30)." unless (3..30) === depth
15
+ @depth = depth
16
+ reseed(seed)
17
+ end
18
+
19
+ #Set up a new seed value
20
+ def reseed(seed=Time.now.to_s)
21
+ @buffer = Array.new(@depth+2, 0)
22
+ seedsrc = (seed.to_s + seed.class.to_s + 'Leonardo Pisano').each_byte.cycle
23
+ indxsrc = (0...depth).cycle
24
+
25
+ 1024.times do
26
+ @buffer[indxsrc.next] += seedsrc.next
27
+ do_spin
28
+ end
29
+ end
30
+
31
+ #Roll a dice.
32
+ def dice(sides)
33
+ do_spin
34
+ @buffer[0] % sides
35
+ end
36
+
37
+ #Get a pseudo random byte
38
+ def byte
39
+ do_spin
40
+ @buffer[0] & BYTE
41
+ end
42
+
43
+ #Get a pseudo random word
44
+ def byte
45
+ do_spin
46
+ @buffer[0] & WORD
47
+ end
48
+
49
+ #Get a pseudo random float
50
+ def byte
51
+ do_spin
52
+ @buffer[0].to_f / BASE
53
+ end
54
+
55
+ #Cycle through the PRNG count times.
56
+ def spin(count=1)
57
+ count.times do
58
+ do_spin
59
+ end
60
+ end
61
+
62
+ private
63
+ #Cycle through the PRNG once.
64
+ def do_spin
65
+ buffer[-2] = buffer[0]
66
+ buffer[-1] = buffer[1]
67
+
68
+ 0...depth do |i|
69
+ buffer[i] = (buffer[i+1] + (buffer[i+2] >> 1)) & CHOP
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,3 @@
1
+ class FibonacciRng
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fibonacci_rng
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Camilleri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A Fibonacci inspired pseudo random number generator. Just try and prove
42
+ that is sucks! ;-)
43
+ email:
44
+ - peter.c.camilleri@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - fibonacci_rng.gemspec
55
+ - lib/fibonacci_rng.rb
56
+ - lib/fibonacci_rng/version.rb
57
+ homepage: ''
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.1.4
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: A Fibonacci inspired pseudo random number generator.
81
+ test_files: []
82
+ has_rdoc: