Class: SuperRandom
- Inherits:
-
Object
- Object
- SuperRandom
- 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
-
#byte_count ⇒ Object
readonly
Returns the value of attribute byte_count.
-
#first_timeout ⇒ Object
Returns the value of attribute first_timeout.
-
#nevermind ⇒ Object
Returns the value of attribute nevermind.
-
#second_timeout ⇒ Object
Returns the value of attribute second_timeout.
-
#source_count ⇒ Object
readonly
Returns the value of attribute source_count.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Instance Method Summary collapse
-
#bytes ⇒ Object
Returns 64 random bytes(at least from SecureRandom’s).
- #hexadecimal ⇒ Object
-
#initialize(*sources) ⇒ SuperRandom
constructor
A new instance of SuperRandom.
- #random_number(scale = 1.0) ⇒ Object (also: #rand)
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_count ⇒ Object (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_timeout ⇒ Object
Returns the value of attribute first_timeout.
18 19 20 |
# File 'lib/super_random.rb', line 18 def first_timeout @first_timeout end |
#nevermind ⇒ Object
Returns the value of attribute nevermind.
18 19 20 |
# File 'lib/super_random.rb', line 18 def nevermind @nevermind end |
#second_timeout ⇒ Object
Returns the value of attribute second_timeout.
18 19 20 |
# File 'lib/super_random.rb', line 18 def second_timeout @second_timeout end |
#source_count ⇒ Object (readonly)
Returns the value of attribute source_count.
19 20 21 |
# File 'lib/super_random.rb', line 19 def source_count @source_count end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
19 20 21 |
# File 'lib/super_random.rb', line 19 def sources @sources end |
Instance Method Details
#bytes ⇒ Object
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 |
#hexadecimal ⇒ Object
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 |