super_random 1.1.0 → 1.2.200123

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
  SHA256:
3
- metadata.gz: 83691b6150693c16ecc7f602259eceba24a343e7583db204f388015bfe133796
4
- data.tar.gz: efa9698299c61b3df708f3c8392635d76373e4654129ef093737c7da5a1a9e0d
3
+ metadata.gz: aeb192c166b91d82fb0a4b535d851a5479c5bae43bf4a37d607ae199b128d83d
4
+ data.tar.gz: 6fb7ad8629f9775183a65c040dc762d48e178a761c545008028e57c0002162dd
5
5
  SHA512:
6
- metadata.gz: 6c9a8746336cdf86a2643790b0406345ee859ccd5de84868ea834d4e4260908016df5e1b917da9ebbd8848885d7ce1e394c5be17382509b79c773445cbf75dfe
7
- data.tar.gz: 1437d184fbca7782cf65a2f05d7b6cf424e396de112ac2e928db08836171b5d86fc2da5f015e3962155602c59e693fb8eb97dc5bb8b8a639ff4dc39c5cdb86c7
6
+ metadata.gz: c51049b6220c0c971146cd2ddc0463dcbd7037c7bc7695aca8a50653d8acd22cc2964d85f66d667ad311526f2e0a8748d0e4a2e501f9621aa64db0f524d78883
7
+ data.tar.gz: f583c0095ca47c209730afa445bca8304f041d55de8c0f26e599e9cac7770bbdc3f4001e2e9fc82fe8e24ea70a5c9063d01e12e616a6708bc88dcfd71c990f8d
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # SuperRandom
2
2
 
3
- * [github](https://www.github.com/carlosjhr64/super_random)
3
+ * [VERSION 1.2.200123](https://github.com/carlosjhr64/super_random/releases)
4
+ * [github](https://github.com/carlosjhr64/super_random)
4
5
  * [rubygems](https://rubygems.org/gems/super_random)
5
6
 
6
7
  ## DESCRIPTION:
@@ -11,18 +12,26 @@ SuperRandom combines five online real random services to create a more perfect r
11
12
 
12
13
  ## SYNOPSIS:
13
14
 
14
- require 'super_random' #=> true
15
- super_random = SuperRandom.new # => #<SuperRandom:...
15
+ require 'super_random'
16
+ super_random = SuperRandom.new
16
17
 
17
- # bytes returns 32 bytes by default (256 bits).
18
- super_random.bytes # => [123, 219, 128, ..., 248, 164, 100]
18
+ super_random.bytes(32) #~> ^\[\d+(, \d+){31}\]$
19
+ # Example:
20
+ # [142, 36, 107, 199, 1, 222, 69, 238, 130, 159, 236, 201, 199,
21
+ # 33, 237, 166, 189, 166, 95, 246, 111, 103, 113, 126, 27, 31,
22
+ # 244, 215, 200, 60, 255, 184]
19
23
 
20
- # hexadecimal returns a 32 bytes hexadecimal by default.
21
- super_random.hexadecimal #=> "2ae...37b"
24
+ super_random.hexadecimal(32) #~> ^\h{64}$
25
+ # Example:
26
+ # "3e0dffe42c08b849dc3c1290e7aa87dff4ad3037b29694136786a4db1e3efab8"
22
27
 
23
- # rand as the typical use
24
- super_random.rand #=> 0.16882225652425537
25
- super_random.rand(100) #=> 85
28
+ super_random.random_number(100.0) #~> ^\d{1,2}\.\d+$
29
+ # Example:
30
+ # 16.882225652425537
31
+
32
+ super_random.random_number(100) #~> ^\d{1,2}$
33
+ # Example:
34
+ # 85
26
35
 
27
36
  # The "services" attribute gives the number of online services used.
28
37
  # It's possible for a service to fail.
@@ -37,7 +46,7 @@ SuperRandom combines five online real random services to create a more perfect r
37
46
 
38
47
  (The MIT License)
39
48
 
40
- Copyright (c) 2017 carlosjhr64
49
+ Copyright (c) 2020 carlosjhr64
41
50
 
42
51
  Permission is hereby granted, free of charge, to any person obtaining
43
52
  a copy of this software and associated documentation files (the
@@ -1,5 +1,5 @@
1
1
  class SuperRandom
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.200123'
3
3
  end
4
4
 
5
5
  # Standard Libraries
@@ -1,4 +1,5 @@
1
1
  class SuperRandom
2
+ DEFAULT_BYTES = 32
2
3
 
3
4
  # http://qrng.anu.edu.au/index.php
4
5
  # https://qrng.anu.edu.au/API/api-demo.php
@@ -77,7 +78,7 @@ class SuperRandom
77
78
  @services = 0
78
79
  end
79
80
 
80
- def bytes(n=32)
81
+ def bytes(n=DEFAULT_BYTES)
81
82
  @randomness = 0.0
82
83
  @services = 0
83
84
 
@@ -122,13 +123,43 @@ class SuperRandom
122
123
  return a
123
124
  end
124
125
 
125
- def hexadecimal(n=32)
126
+ def hexadecimal(n=DEFAULT_BYTES)
126
127
  bytes(n).map{|i|i.to_s(16).rjust(2,'0')}.join
127
128
  end
128
129
 
129
- def rand(m=nil, n=6)
130
- div = n.times.inject(''){|s,i| s+'FF'}.to_i(16).to_f
131
- r = hexadecimal(n).to_i(16).to_f / div
132
- m.nil? ? r : (m*r).to_i
130
+ def random_number(scale=1.0, minbytes=6, maxbytes=[minbytes,DEFAULT_BYTES].max)
131
+ case scale
132
+ when Float
133
+ div = minbytes.times.inject(''){|s,i| s+'FF'}.to_i(16)
134
+ den = hexadecimal(minbytes).to_i(16)
135
+ return scale * den.to_f / div.to_f
136
+ when Integer
137
+ n = n0 = Math.log(scale, 256).ceil
138
+ e = e0 = 256**n
139
+ r = r0 = e0 % scale
140
+ while r > 0
141
+ n0 += 1
142
+ e0 *= 256
143
+ r0 = e0 % scale
144
+ if r0 <= r
145
+ # break if repeating pattern with big enough integer
146
+ break if r0 == r and n0 > minbytes
147
+ r,n,e = r0,n0,e0
148
+ end
149
+ break if n0 >= maxbytes
150
+ end
151
+ max = (e/scale)*scale
152
+ loop do
153
+ number = hexadecimal(n).to_i(16)
154
+ return number % scale if number < max
155
+ # On a relatively small chance that we're above max...
156
+ if @nevermind
157
+ warn "using SecureRandom.random_number(#{scale})"
158
+ return SecureRandom.random_number(scale)
159
+ end
160
+ end
161
+ end
162
+ raise "rand(scale Integer|Float)"
133
163
  end
164
+ alias rand random_number
134
165
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super_random
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.200123
5
5
  platform: ruby
6
6
  authors:
7
7
  - carlosjhr64
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-18 00:00:00.000000000 Z
11
+ date: 2020-01-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  You can't get more random than random, but you can try really, really, really hard.
@@ -41,8 +41,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  requirements:
44
- - 'ruby: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]'
45
- rubygems_version: 3.0.3
44
+ - 'ruby: ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]'
45
+ rubygems_version: 3.1.2
46
46
  signing_key:
47
47
  specification_version: 4
48
48
  summary: You can't get more random than random, but you can try really, really, really