super_random 3.0.230113 → 3.1.230114

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -3
  3. data/lib/super_random.rb +43 -3
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 313ffa9d968e2c7b511fc80ec9a0b6c3de755fe0dd9ed51531448e9f8768d393
4
- data.tar.gz: 43cae93ca8d339bd61018fa3b93806426ad7a8bc77161a52309e23b99f0aeedc
3
+ metadata.gz: da086dfbbc9f424f5c8f7f35e38fc1be67633986797aea732af695c0ed4a8b67
4
+ data.tar.gz: ed4274a6e0b5b113c37c9adc4b20f9f96eacdeb5798e79235d023b6c5683f847
5
5
  SHA512:
6
- metadata.gz: 262c9131cc290b6df1cb1b68b54aaa2fb0cd60b6288c33facd07761e1f80802747a0555db7ec7edb9dd5146ecd088c463b5347a5dce2f2da263518964740e3a0
7
- data.tar.gz: 352bcace20e9b6f2250c55000a69d2fa51c5f8db3b8a0f7c7474be068d2698d0622d7759c9acfd3cb2e4fa2c2b0fb3a81bfd60fee7390562c1b0078f001f4c84
6
+ metadata.gz: 78284485c7c649b00f1916d6a7b946f9673ab223f01f8b28bc227975da9154ccbc87bfb1af76eb36a0ad378c4c222c9135b239cad57b655f02d6860c146f9205
7
+ data.tar.gz: bab65a30405474b92539bfffdbd82fca278882c0be7d45f74d12c57d2c0f7ffc874cfd7e9ba0c0d1cc5b949d909adf298aa036b9974cf35c8dd248340f929be3
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SuperRandom
2
2
 
3
- * [VERSION 3.0.230113](https://github.com/carlosjhr64/super_random/releases)
3
+ * [VERSION 3.1.230114](https://github.com/carlosjhr64/super_random/releases)
4
4
  * [github](https://github.com/carlosjhr64/super_random)
5
5
  * [rubygems](https://rubygems.org/gems/super_random)
6
6
 
@@ -17,7 +17,7 @@ You can't get more random than random, but you can try really, really, really ha
17
17
  ## SYNOPSIS:
18
18
  ```ruby
19
19
  require 'super_random'
20
- SuperRandom::VERSION #=> "3.0.230113"
20
+ SuperRandom::VERSION #=> "3.1.230114"
21
21
  SuperRandom::DEFAULT_SOURCES #~> www.random.org
22
22
  super_random = SuperRandom.new
23
23
  super_random.sources #~> www.random.org
@@ -34,7 +34,10 @@ super_random.source_count #=> 0
34
34
  super_random.byte_count #=> 0
35
35
  # Snapshots from your webcam can be a good entropy source:
36
36
  super_random = SuperRandom.new('/var/lib/motion/lastsnap.jpg')
37
- super_random.sources #=> ["/var/lib/motion/lastsnap.jpg"]
37
+ super_random.sources #=> ["/var/lib/motion/lastsnap.jpg"]
38
+ # Dice!
39
+ d6 = super_random.dice(6)
40
+ d6.roll #~> ^[123456]$
38
41
  ```
39
42
  ## METHODOLOGY:
40
43
 
data/lib/super_random.rb CHANGED
@@ -6,7 +6,7 @@ require 'digest'
6
6
  #`ruby`
7
7
 
8
8
  class SuperRandom
9
- VERSION = '3.0.230113'
9
+ VERSION = '3.1.230114'
10
10
  DEFAULT_SOURCES = [
11
11
  'https://www.random.org/strings/?num=10&len=20&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new',
12
12
  ]
@@ -34,17 +34,57 @@ class SuperRandom
34
34
  bytes.map{_1.to_s(16).rjust(2,'0')}.join
35
35
  end
36
36
 
37
+ def integer
38
+ hexadecimal.to_i(16)
39
+ end
40
+
41
+ def float
42
+ Rational(integer, DIV).to_f
43
+ end
44
+
45
+ def float2
46
+ # An alternate way to generate a float but...
47
+ hex = hexadecimal
48
+ x = hex[0...64].to_i(16)
49
+ y = hex[64..128].to_i(16)
50
+ # ...what distribution is this?
51
+ Rational(x,y).to_f
52
+ rescue ZeroDivisionError
53
+ # Fat chance!
54
+ return 0 if x == 0
55
+ 1.0/0.0
56
+ end
57
+
37
58
  def random_number(scale=1.0)
38
59
  case scale
39
60
  when Float
40
- return scale * Rational(hexadecimal.to_i(16), DIV)
61
+ return scale * float
41
62
  when Integer
42
- return ((hexadecimal.to_i(16) + SecureRandom.random_number(scale)) % scale)
63
+ return ((integer + SecureRandom.random_number(scale)) % scale)
43
64
  end
44
65
  raise "rand(scale Integer|Float)"
45
66
  end
46
67
  alias rand random_number
47
68
 
69
+ class Dice
70
+ private def set_big
71
+ @big = @rng.integer + SecureRandom.random_number(@sides)
72
+ end
73
+ def initialize(sides, minimum:1, rng:SuperRandom.new)
74
+ @sides,@minimum,@rng = sides,minimum,rng
75
+ set_big
76
+ end
77
+ def roll
78
+ @big,roll = @big.divmod(@sides)
79
+ return roll+@minimum
80
+ ensure
81
+ set_big unless @big>0
82
+ end
83
+ end
84
+ def dice(sides, minimum:1)
85
+ Dice.new(sides, minimum:minimum, rng:self)
86
+ end
87
+
48
88
  private
49
89
 
50
90
  SHASUM = Digest::SHA2.new(512)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super_random
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.230113
4
+ version: 3.1.230114
5
5
  platform: ruby
6
6
  authors:
7
7
  - CarlosJHR64
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-13 00:00:00.000000000 Z
11
+ date: 2023-01-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  You can't get more random than random, but you can try really, really, really hard.