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.230213'- 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.
23 24 25 26 27 |
# File 'lib/super_random.rb', line 23 def initialize(*sources) @sources = sources.empty? ? DEFAULT_SOURCES.dup : sources @byte_count,@source_count = 0,0 @first_timeout,@second_timeout,@nevermind = 3,3,true end |
Instance Attribute Details
#byte_count ⇒ Object (readonly)
Returns the value of attribute byte_count.
21 22 23 |
# File 'lib/super_random.rb', line 21 def byte_count @byte_count end |
#first_timeout ⇒ Object
Returns the value of attribute first_timeout.
20 21 22 |
# File 'lib/super_random.rb', line 20 def first_timeout @first_timeout end |
#nevermind ⇒ Object
Returns the value of attribute nevermind.
20 21 22 |
# File 'lib/super_random.rb', line 20 def nevermind @nevermind end |
#second_timeout ⇒ Object
Returns the value of attribute second_timeout.
20 21 22 |
# File 'lib/super_random.rb', line 20 def second_timeout @second_timeout end |
#source_count ⇒ Object (readonly)
Returns the value of attribute source_count.
21 22 23 |
# File 'lib/super_random.rb', line 21 def source_count @source_count end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
21 22 23 |
# File 'lib/super_random.rb', line 21 def sources @sources end |
Instance Method Details
#bytes ⇒ Object
Returns 64 random bytes(at least from SecureRandom’s)
30 31 32 33 34 |
# File 'lib/super_random.rb', line 30 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
95 96 97 |
# File 'lib/super_random.rb', line 95 def dice(sides, minimum:1) Dice.new(sides, minimum:minimum, rng:self) end |
#float ⇒ Object
58 59 60 |
# File 'lib/super_random.rb', line 58 def float Rational(integer, DIV).to_f end |
#float2 ⇒ Object
62 63 64 65 66 67 |
# File 'lib/super_random.rb', line 62 def float2 # An alternate way to generate a float... x,y = xy # ...but what distribution is this? Rational(x+1,y+1).to_f end |
#hexadecimal ⇒ Object
36 37 38 |
# File 'lib/super_random.rb', line 36 def hexadecimal bytes.map{_1.to_s(16).rjust(2,'0')}.join end |
#integer ⇒ Object
40 41 42 |
# File 'lib/super_random.rb', line 40 def integer hexadecimal.to_i(16) end |
#integer2 ⇒ Object
51 52 53 54 55 56 |
# File 'lib/super_random.rb', line 51 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
69 70 71 72 73 74 75 76 77 |
# File 'lib/super_random.rb', line 69 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
44 45 46 47 48 49 |
# File 'lib/super_random.rb', line 44 def xy hex = hexadecimal x = hex[0...64].to_i(16) y = hex[64..128].to_i(16) [x,y] end |