Class: SuperRandom
- Inherits:
-
Object
- Object
- SuperRandom
- Defined in:
- lib/super_random/version.rb,
lib/super_random/super_random.rb
Constant Summary collapse
- VERSION =
'0.1.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
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.
13 14 15 16 17 18 19 |
# File 'lib/super_random/super_random.rb', line 13 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.
10 11 12 |
# File 'lib/super_random/super_random.rb', line 10 def first_timeout @first_timeout end |
#nevermind ⇒ Object
Returns the value of attribute nevermind.
10 11 12 |
# File 'lib/super_random/super_random.rb', line 10 def nevermind @nevermind end |
#randomness ⇒ Object (readonly)
Returns the value of attribute randomness.
11 12 13 |
# File 'lib/super_random/super_random.rb', line 11 def randomness @randomness end |
#second_timeout ⇒ Object
Returns the value of attribute second_timeout.
10 11 12 |
# File 'lib/super_random/super_random.rb', line 10 def second_timeout @second_timeout end |
#services ⇒ Object (readonly)
Returns the value of attribute services.
11 12 13 |
# File 'lib/super_random/super_random.rb', line 11 def services @services end |
Class Method Details
.randbyte(r, s) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/super_random/super_random.rb', line 3 def self.randbyte(r,s) return r.randbyte rescue Exception warn "RealRand's #{s} failed." return nil end |
Instance Method Details
#bytes(n = 32) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/super_random/super_random.rb', line 21 def bytes(n=32) @randomness = 0.0 @services = 0 r1 = RealRand::RandomOrg.new r2 = RealRand::EntropyPool.new r3 = RealRand::FourmiLab.new a1 = a2 = a3 = nil t1 = Thread.new{ a1 = SuperRandom.randbyte(r1,'RandomOrg')[0...n] } t2 = Thread.new{ a2 = SuperRandom.randbyte(r2,'EntropyPool')[0...n] } t3 = Thread.new{ a3 = SuperRandom.randbyte(r3,'FourmiLab')[0...n] } begin Timeout.timeout(@first_timeout) do # Initially, would like to get them all. t1.join and t2.join and t3.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 (t1.alive? or t2.alive? or t3.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].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
68 69 70 |
# File 'lib/super_random/super_random.rb', line 68 def hexadecimal(n=32) bytes(n).map{|i|i.to_s(16).rjust(2,'0')}.join end |
#rand(m = nil, n = 6) ⇒ Object
72 73 74 75 76 |
# File 'lib/super_random/super_random.rb', line 72 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 |