Class: SuperRandom

Inherits:
Object
  • Object
show all
Defined in:
lib/super_random.rb

Overview

‘ruby`

Constant Summary collapse

VERSION =
'3.0.230113'
DEFAULT_SOURCES =
[
  'https://www.random.org/strings/?num=10&len=20&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new',
]
MUTEX =
Mutex.new
DIV =
128.times.inject(''){|h,_|h<<'f'}.to_i(16)
RATE_LIMIT =

One minute, be nice!

60
@@LAST_TIME =
Time.now.to_i - RATE_LIMIT - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*sources) ⇒ SuperRandom

Returns a new instance of SuperRandom.



20
21
22
23
24
# File 'lib/super_random.rb', line 20

def initialize(*sources)
  @sources = sources.empty? ? DEFAULT_SOURCES.dup : sources
  @byte_count,@source_count = 0,0
  @first_timeout,@second_timeout,@nevermind = 3,6,true
end

Instance Attribute Details

#byte_countObject (readonly)

Returns the value of attribute byte_count.



19
20
21
# File 'lib/super_random.rb', line 19

def byte_count
  @byte_count
end

#first_timeoutObject

Returns the value of attribute first_timeout.



18
19
20
# File 'lib/super_random.rb', line 18

def first_timeout
  @first_timeout
end

#nevermindObject

Returns the value of attribute nevermind.



18
19
20
# File 'lib/super_random.rb', line 18

def nevermind
  @nevermind
end

#second_timeoutObject

Returns the value of attribute second_timeout.



18
19
20
# File 'lib/super_random.rb', line 18

def second_timeout
  @second_timeout
end

#source_countObject (readonly)

Returns the value of attribute source_count.



19
20
21
# File 'lib/super_random.rb', line 19

def source_count
  @source_count
end

#sourcesObject (readonly)

Returns the value of attribute sources.



19
20
21
# File 'lib/super_random.rb', line 19

def sources
  @sources
end

Instance Method Details

#bytesObject

Returns 64 random bytes(at least from SecureRandom’s)



27
28
29
30
31
# File 'lib/super_random.rb', line 27

def bytes
  @byte_count,@source_count = 0,0
  _do_updates if Time.now.to_i - @@LAST_TIME > RATE_LIMIT
  _get_bytes
end

#hexadecimalObject



33
34
35
# File 'lib/super_random.rb', line 33

def hexadecimal
  bytes.map{_1.to_s(16).rjust(2,'0')}.join
end

#random_number(scale = 1.0) ⇒ Object Also known as: rand



37
38
39
40
41
42
43
44
45
# File 'lib/super_random.rb', line 37

def random_number(scale=1.0)
  case scale
  when Float
    return scale * Rational(hexadecimal.to_i(16), DIV)
  when Integer
    return ((hexadecimal.to_i(16) + SecureRandom.random_number(scale)) % scale)
  end
  raise "rand(scale Integer|Float)"
end