Class: SuperRandom
- Inherits:
-
Object
- Object
- SuperRandom
- Defined in:
- lib/super_random.rb,
lib/super_random/super_random.rb
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Attribute Summary collapse
-
#first_timeout ⇒ Object
Returns the value of attribute first_timeout.
-
#nevermind ⇒ Object
Returns the value of attribute nevermind.
-
#randomness ⇒ Object
readonly
Returns the value of attribute randomness.
-
#second_timeout ⇒ Object
Returns the value of attribute second_timeout.
-
#services ⇒ Object
readonly
Returns the value of attribute services.
Class Method Summary collapse
- .atmospheric(n) ⇒ Object
- .entropy_pool(n) ⇒ Object
- .hotbits(n, k = 'Pseudorandom') ⇒ Object
- .quantum(n) ⇒ Object
Instance Method Summary collapse
- #bytes(n = 32) ⇒ Object
- #hexadecimal(n = 32) ⇒ Object
-
#initialize ⇒ SuperRandom
constructor
A new instance of SuperRandom.
- #rand(m = nil, n = 6) ⇒ Object
Constructor Details
#initialize ⇒ SuperRandom
Returns a new instance of SuperRandom.
53 54 55 56 57 58 59 |
# File 'lib/super_random/super_random.rb', line 53 def initialize @first_timeout = 3 @second_timeout = 6 @nevermind = true @randomness = 0.0 @services = 0 end |
Instance Attribute Details
#first_timeout ⇒ Object
Returns the value of attribute first_timeout.
50 51 52 |
# File 'lib/super_random/super_random.rb', line 50 def first_timeout @first_timeout end |
#nevermind ⇒ Object
Returns the value of attribute nevermind.
50 51 52 |
# File 'lib/super_random/super_random.rb', line 50 def nevermind @nevermind end |
#randomness ⇒ Object (readonly)
Returns the value of attribute randomness.
51 52 53 |
# File 'lib/super_random/super_random.rb', line 51 def randomness @randomness end |
#second_timeout ⇒ Object
Returns the value of attribute second_timeout.
50 51 52 |
# File 'lib/super_random/super_random.rb', line 50 def second_timeout @second_timeout end |
#services ⇒ Object (readonly)
Returns the value of attribute services.
51 52 53 |
# File 'lib/super_random/super_random.rb', line 51 def services @services end |
Class Method Details
.atmospheric(n) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/super_random/super_random.rb', line 15 def self.atmospheric(n) s = Net::HTTP.get(URI( "https://www.random.org/integers/?num=#{n}&min=0&max=255&col=1&base=10&format=plain&rnd=new")) a = s.strip.split(/\s+/).map{|j|j.to_i} raise unless a.length==n raise unless a.all?{|i| i>-1 and i<256} return a rescue StandardError warn "atmospheric (www.random.org) failed." return nil end |
.entropy_pool(n) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/super_random/super_random.rb', line 27 def self.entropy_pool(n) s = Net::HTTP.get(URI( "http://random.hd.org/getBits.jsp?numBytes=#{n}&type=bin")) a = s.bytes # As of the time of this writting, not guaranteed to get more than 8 bytes. raise unless (n>8)? a.length.between?(8,n) : a.length==n return a rescue StandardError warn "entropy_pool (random.hd.org) failed." return nil end |
.hotbits(n, k = 'Pseudorandom') ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/super_random/super_random.rb', line 39 def self.hotbits(n, k='Pseudorandom') s = Net::HTTP.get(URI( "https://www.fourmilab.ch/cgi-bin/Hotbits.api?nbytes=#{n}&fmt=bin&apikey=#{k}")) a = s.bytes raise unless a.length==n return a rescue StandardError warn "hotbits (www.fourmilab.ch) failed." return nil end |
.quantum(n) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/super_random/super_random.rb', line 3 def self.quantum(n) s = Net::HTTP.get(URI( "https://qrng.anu.edu.au/API/jsonI.php?length=#{n}&type=uint8")) a = JSON.parse(s)['data'] raise unless a.is_a?(Array) and a.length==n raise unless a.all?{|i| i.is_a?(Integer) and i>-1 and i<256} return a rescue StandardError warn "quantum (qrng.anu.edu.au) failed." return nil end |
Instance Method Details
#bytes(n = 32) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/super_random/super_random.rb', line 61 def bytes(n=32) @randomness = 0.0 @services = 0 a1 = a2 = a3 = a4 = nil t1 = Thread.new{ a1 = SuperRandom.quantum(n)} t2 = Thread.new{ a2 = SuperRandom.atmospheric(n)} t3 = Thread.new{ a3 = SuperRandom.entropy_pool(n)} t4 = Thread.new{ a4 = SuperRandom.hotbits(n)} begin Timeout.timeout(@first_timeout) do # Initially, would like to get them all. t1.join and t2.join and t3.join and t4.join end rescue Timeout::Error begin Timeout.timeout(@second_timeout) do # But at this point, # would like to get at least one. while a1.nil? and a2.nil? and a3.nil? and a4.nil? (t1.alive? or t2.alive? or t3.alive? or t4.alive?) Thread.pass end end rescue Timeout::Error # If we don't care that we got nothing, go on. raise $! unless @nevermind end end a = n.times.inject([]){|b,i|b.push(SecureRandom.random_number(256))} [a1, a2, a3, a4].each do |b| if b bl = b.length @randomness += bl.to_f/n.to_f @services += 1 n.times{|i|a[i]=(a[i]+b[i%bl])%256} end end return a end |
#hexadecimal(n = 32) ⇒ Object
106 107 108 |
# File 'lib/super_random/super_random.rb', line 106 def hexadecimal(n=32) bytes(n).map{|i|i.to_s(16).rjust(2,'0')}.join end |
#rand(m = nil, n = 6) ⇒ Object
110 111 112 113 114 |
# File 'lib/super_random/super_random.rb', line 110 def rand(m=nil, n=6) div = n.times.inject(''){|s,i| s+'FF'}.to_i(16).to_f r = hexadecimal(n).to_i(16).to_f / div m.nil? ? r : (m*r).to_i end |