fibonacci_rng 1.2.0 → 1.2.1

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: 5064c4df22e9527ed30a68a7c7b4470721fd2e6c
4
- data.tar.gz: a4f2d50bb58bf04fac0f66f7dfd57b7f0be5a6a7
3
+ metadata.gz: 22cbdf19766e5f31416393eeabb3a5b60aa68fa9
4
+ data.tar.gz: e6deeb3eaeaca2700613ed28b74dbcc83ce67c3e
5
5
  SHA512:
6
- metadata.gz: a4c4f246d53887844cb1e0a42cce67a99519da7c6c6070a5ca7de651c8468a87925d93f5648ae72714bdbf196700dd8e02e10a412f7990786d20f0b97ebb9a30
7
- data.tar.gz: 5896d20ae29c6798376fc6961c08e1bd2eceff8eb2626a2f08a3f71c7a2e345d3a8da97efb1c21818a4a81b27e25af371753fd08af28795fd7301927228e337a
6
+ metadata.gz: 641404c311f664c1b56c37415054d133ea6376afafaacd44ddb1456f33bfd81b6262ac366a96d92de2142e543dd53ca23f971200323e99d3575ec334ca273187
7
+ data.tar.gz: accb5987d69ba8c2623ef7fcdecdf69a409c8be30a140d9d84a082fa3aea68ab8ab72138490ddd39c5c6c9fb1e14d15b4c7ee0f0e5aa2bf822f3a258a29a9a71
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ devkit.bat
data/README.md CHANGED
@@ -120,7 +120,8 @@ In addition, here are some other options:
120
120
  @my_rng.dice(100) # A "random" integer between 0 and 99
121
121
  @my_rng.byte # A "random" integer between 0 and 255
122
122
  @my_rng.word # A "random" integer between 0 and 65535
123
- @my_rng.float # A "random" float between 0 and less than 1.
123
+ @my_rng.float # A quick "random" float between 0 and less than 1.
124
+ @my_rng.double # A better "random" float between 0 and less than 1.
124
125
  @my_rng.string(10) # A "random" string of 10 characters in length.
125
126
 
126
127
  # A "random" string of 10 characters in length from the string 'abcdefg'.
@@ -136,6 +137,9 @@ and also available are these helpful methods:
136
137
  @my_rng.spin # Spin the generator once.
137
138
  ```
138
139
 
140
+ Note: reseed is an alias of the srand method. The reseed method is preferred
141
+ due to its clearer name.
142
+
139
143
  If more than one stream of numbers is required, it is best to use multiple
140
144
  instances of FibonacciRng objects rather than rely on one. This will help avoid
141
145
  the two streams of data being correlated.
@@ -192,8 +196,7 @@ Each time any of these is run, a different salt string will be generated.
192
196
  not. However, it doesn't matter either. The bytes method was added to improve
193
197
  interoperability with the standard Random class. Thus deprecating it was a
194
198
  mistake. This note serves to announce that the bytes method is not going away.
195
- The source code will say it is deprecated until the next code update, but that
196
- comment is in error.
199
+
197
200
 
198
201
  ## Theory of Operation
199
202
 
data/lib/fibonacci_rng.rb CHANGED
@@ -14,6 +14,7 @@ class FibonacciRng
14
14
  BYTE = 0xFF
15
15
  WORD = 0xFFFF
16
16
  BASE = (CHOP+1).to_f
17
+ DOUBLE = BASE*BASE
17
18
  DEPTHS = 2..256
18
19
  INITS = 1..1_000_000
19
20
 
@@ -53,6 +53,14 @@ class FibonacciRng
53
53
  @buffer[0].to_f / BASE
54
54
  end
55
55
 
56
+ #Get a better pseudo random float
57
+ def double
58
+ do_spin
59
+ part_one = @buffer[0].to_f * BASE
60
+ do_spin
61
+ (part_one + @buffer[0].to_f) / DOUBLE
62
+ end
63
+
56
64
  #The printable seven bit ASCII characters.
57
65
  ASCII_7_BIT = (' '..'~').to_a.join
58
66
 
@@ -12,6 +12,8 @@ class FibonacciRng
12
12
  do_reseed(indxsrc, seedsrc)
13
13
  end
14
14
 
15
+ alias :reseed :srand
16
+
15
17
  private
16
18
 
17
19
  #A patch for the seeder class name bug.
@@ -2,5 +2,5 @@
2
2
 
3
3
  #The class of Fibonacci inspired random number generators.
4
4
  class FibonacciRng
5
- VERSION = "1.2.0"
5
+ VERSION = "1.2.1"
6
6
  end
@@ -186,8 +186,63 @@ class FibonacciRngTester < Minitest::Test
186
186
 
187
187
  prng = FibonacciRng.new("%s*08^_Tg{NnirtZ-94)q9z2l+~bB5")
188
188
  result = Array.new(80) { prng.byte }
189
+ assert_equal(expected, result)
189
190
 
191
+ prng.reseed("%s*08^_Tg{NnirtZ-94)q9z2l+~bB5")
192
+ result = Array.new(80) { prng.byte }
190
193
  assert_equal(expected, result)
194
+
195
+
196
+ expected = [0.466758593916893, 0.8060004059225321,
197
+ 0.8063845634460449, 0.8676037490367889,
198
+ 0.350975576788187, 0.2556227296590805,
199
+ 0.4873242452740669, 0.07484667748212814,
200
+ 0.2968141995370388, 0.5417192056775093,
201
+ 0.4288134817034006, 0.26460993848741055,
202
+ 0.13613684102892876, 0.27074786089360714,
203
+ 0.11685592867434025, 0.814235333353281,
204
+ 0.6137734726071358, 0.9152738898992538,
205
+ 0.6325213424861431, 0.22782298550009727,
206
+ 0.6877559795975685, 0.9354030545800924,
207
+ 0.18385234475135803, 0.5579136144369841,
208
+ 0.7501311469823122, 0.04208622872829437,
209
+ 0.31922253780066967, 0.6471036206930876,
210
+ 0.4305369835346937, 0.2239683922380209,
211
+ 0.9770196247845888, 0.3727417625486851]
212
+
213
+ prng.reseed("%s*08^_Tg{NnirtZ-94)q9z2l+~bB5")
214
+ result = Array.new(32) { prng.float }
215
+ #assert_equal(expected, result)
216
+
217
+ (0...32).each do |i|
218
+ assert_in_delta(expected[i], result[i], 1.0e-16)
219
+ end
220
+
221
+ expected = [0.46675859541818576, 0.8063845650620829,
222
+ 0.3509755772643215, 0.48732424541347974,
223
+ 0.29681420054606944, 0.428813482196275,
224
+ 0.13613684153323594, 0.11685593019097174,
225
+ 0.6137734743119663, 0.6325213429104964,
226
+ 0.6877559813398925, 0.18385234579055312,
227
+ 0.7501311470607039, 0.31922253900599407,
228
+ 0.43053698395186735, 0.9770196254788744,
229
+ 0.105776653432334, 0.17992045162625886,
230
+ 0.7068137351637119, 0.09374992924495854,
231
+ 0.4741257221497737, 0.2717329622804037,
232
+ 0.6427948478921109, 0.048162101812947195,
233
+ 0.649627174383166, 0.27438020721066836,
234
+ 0.9478733559036244, 0.6199505789910625,
235
+ 0.8043054302444751, 0.9363898295339244,
236
+ 0.6613804354420972, 0.5876014495519649]
237
+
238
+ prng.reseed("%s*08^_Tg{NnirtZ-94)q9z2l+~bB5")
239
+ result = Array.new(32) { prng.double }
240
+ #assert_equal(expected, result)
241
+
242
+ (0...32).each do |i|
243
+ assert_in_delta(expected[i], result[i], 1.0e-16)
244
+ end
245
+
191
246
  end
192
247
 
193
248
  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: 1.2.0
4
+ version: 1.2.1
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-07-12 00:00:00.000000000 Z
11
+ date: 2016-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest_visible