Class: SuperRandom
- Inherits:
-
Object
- Object
- SuperRandom
- Defined in:
- lib/super_random.rb
Overview
ruby
Defined Under Namespace
Classes: Dice
Constant Summary collapse
- VERSION =
'3.2.230116'- 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).
- #dice(sides, minimum: 1) ⇒ Object
- #float ⇒ Object
- #float2 ⇒ Object
- #hexadecimal ⇒ Object
-
#initialize(*sources) ⇒ SuperRandom
constructor
A new instance of SuperRandom.
- #integer ⇒ Object
- #integer2 ⇒ Object
- #random_number(scale = 1.0) ⇒ Object (also: #rand)
- #xy ⇒ Object
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 |
#dice(sides, minimum: 1) ⇒ Object
96 97 98 |
# File 'lib/super_random.rb', line 96 def dice(sides, minimum:1) Dice.new(sides, minimum:minimum, rng:self) end |
#float ⇒ Object
55 56 57 |
# File 'lib/super_random.rb', line 55 def float Rational(integer, DIV).to_f end |
#float2 ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/super_random.rb', line 59 def float2 # An alternate way to generate a float... x,y = xy # ...but what distribution is this? Rational(x,y).to_f rescue ZeroDivisionError # Fat chance! return 0 if x == 0 1.0/0.0 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 |
#integer ⇒ Object
37 38 39 |
# File 'lib/super_random.rb', line 37 def integer hexadecimal.to_i(16) end |
#integer2 ⇒ Object
48 49 50 51 52 53 |
# File 'lib/super_random.rb', line 48 def integer2 # An alternate way to generate an integer x,y = xy # I think this is Binomial(Gaussian in the limit) around zero? x - y end |
#random_number(scale = 1.0) ⇒ Object Also known as: rand
70 71 72 73 74 75 76 77 78 |
# File 'lib/super_random.rb', line 70 def random_number(scale=1.0) case scale when Float return scale * float when Integer return ((integer + SecureRandom.random_number(scale)) % scale) end raise "rand(scale Integer|Float)" end |
#xy ⇒ Object
41 42 43 44 45 46 |
# File 'lib/super_random.rb', line 41 def xy hex = hexadecimal x = hex[0...64].to_i(16) y = hex[64..128].to_i(16) return x,y end |