nerd_dice 0.1.2 → 0.2.1
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.github/workflows/main.yml +67 -0
- data/Gemfile.lock +2 -2
- data/README.md +53 -4
- data/bin/nerd_dice_benchmark +108 -0
- data/checksum/nerd_dice-0.2.0.gem.sha256 +1 -0
- data/checksum/nerd_dice-0.2.0.gem.sha512 +1 -0
- data/lib/nerd_dice/configuration.rb +47 -0
- data/lib/nerd_dice/version.rb +1 -1
- data/lib/nerd_dice.rb +148 -19
- data.tar.gz.sig +0 -0
- metadata +7 -2
- metadata.gz.sig +0 -0
- data/.travis.yml +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88266e9421830d581588170f03cab85b95f95b0f3727c943ae36aa590c6ef338
|
4
|
+
data.tar.gz: 98e7fdc98609de1776400f3b4e8d04bbeff92fb0ef06814c06c967abcfd68a48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e0622aaeee447750ab14395eae84241ced009ff12986c29cb44c903669c7d0e5caed1916dd9693db1fc6da9c9af67bbe9ce2c845f9fa17d85c6a9614e9d9d2b
|
7
|
+
data.tar.gz: c9b50c852027d64160a294aaca650ab882423984a8e79159214dc1ec5e8dd5ce7daa3b13507c616b2c8f218f1ade1f752890080ce7e16b52922992d3c8d49b51
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# This is a basic workflow to help you get started with Actions
|
2
|
+
|
3
|
+
name: Nerd Dice CI
|
4
|
+
|
5
|
+
# Controls when the action will run.
|
6
|
+
on: [push, pull_request]
|
7
|
+
|
8
|
+
# Allows you to run this workflow manually from the Actions tab
|
9
|
+
# workflow_dispatch:
|
10
|
+
|
11
|
+
jobs:
|
12
|
+
test_rspec:
|
13
|
+
|
14
|
+
runs-on: ubuntu-latest
|
15
|
+
strategy:
|
16
|
+
matrix:
|
17
|
+
ruby-version: ['3.0', '3.1', '3.2']
|
18
|
+
|
19
|
+
steps:
|
20
|
+
- uses: actions/checkout@v2
|
21
|
+
- name: Set up Ruby
|
22
|
+
uses: ruby/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
ruby-version: ${{ matrix.ruby-version }}
|
25
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
26
|
+
- name: Run tests
|
27
|
+
run: bundle exec rake
|
28
|
+
- name: Coveralls Parallel
|
29
|
+
uses: coverallsapp/github-action@master
|
30
|
+
with:
|
31
|
+
github-token: ${{ secrets.github_token }}
|
32
|
+
flag-name: run-${{ matrix.test_number }}
|
33
|
+
parallel: true
|
34
|
+
|
35
|
+
rubocop:
|
36
|
+
runs-on: ubuntu-latest
|
37
|
+
steps:
|
38
|
+
- uses: actions/checkout@v2
|
39
|
+
- name: Set up Ruby
|
40
|
+
uses: ruby/setup-ruby@v1
|
41
|
+
with:
|
42
|
+
ruby-version: '3.2'
|
43
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
44
|
+
- name: Run rubocop
|
45
|
+
run: bundle exec rubocop --parallel
|
46
|
+
|
47
|
+
benchmark:
|
48
|
+
runs-on: ubuntu-latest
|
49
|
+
steps:
|
50
|
+
- uses: actions/checkout@v2
|
51
|
+
- name: Set up Ruby
|
52
|
+
uses: ruby/setup-ruby@v1
|
53
|
+
with:
|
54
|
+
ruby-version: '3.2'
|
55
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
56
|
+
- name: Run benchmark script
|
57
|
+
run: bin/nerd_dice_benchmark
|
58
|
+
|
59
|
+
finish:
|
60
|
+
needs: test_rspec
|
61
|
+
runs-on: ubuntu-latest
|
62
|
+
steps:
|
63
|
+
- name: Coveralls Finished
|
64
|
+
uses: coverallsapp/github-action@master
|
65
|
+
with:
|
66
|
+
github-token: ${{ secrets.github_token }}
|
67
|
+
parallel-finished: true
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
[](https://coveralls.io/github/statelesscode/nerd_dice?branch=master)
|
2
|
+

|
3
|
+
[](https://codeclimate.com/github/statelesscode/nerd_dice/maintainability)
|
1
4
|
# NerdDice
|
2
5
|
Nerd dice allows you to roll polyhedral dice and add bonuses as you would in a tabletop roleplaying game. You can choose to roll multiple dice and keep a specified number of dice such as rolling 4d6 and dropping the lowest for ability scores or rolling with advantage and disadvantage if those mechanics exist in your game.
|
3
6
|
|
@@ -18,18 +21,64 @@ Or install it yourself as:
|
|
18
21
|
$ gem install nerd_dice
|
19
22
|
|
20
23
|
## Usage
|
24
|
+
### Configuration
|
25
|
+
You can customize the behavior of NerdDice via a configuration block as below or by assigning an individual property via the ```NerdDice.configuration.property = value``` syntax \(where ```property``` is the config property and ```value``` is the value you want to assign\)\. The available configuration options as well as their defaults, if applicable, are listed in the example configuration block below:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
NerdDice.configure do | config|
|
29
|
+
|
30
|
+
# number of ability scores to place in an ability score array
|
31
|
+
config.ability_score_array_size = 6
|
32
|
+
|
33
|
+
# randomization technique options are:
|
34
|
+
# :securerandom => Uses SecureRandom.rand(). Good entropy, medium speed.
|
35
|
+
# :random_rand => Uses Random.rand(). Class method. Poor entropy, fastest speed.
|
36
|
+
# (Seed is shared with other processes. Too predictable)
|
37
|
+
# :random_object => Uses Random.new() and calls rand()
|
38
|
+
# Medium entropy, fastest speed. (Performs the best under speed benchmark)
|
39
|
+
# :randomized => Uses a random choice of the :securerandom, :rand, and :random_new_interval options above
|
40
|
+
config.randomization_technique = :random_object # fast with independent seed
|
41
|
+
|
42
|
+
# Number of iterations to use on a generator before refreshing the seed
|
43
|
+
# 1 very slow and heavy pressure on processor and memory but very high entropy
|
44
|
+
# 1000 would refresh the object every 1000 times you call rand()
|
45
|
+
config.refresh_seed_interval = nil # don't refresh the seed
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
21
49
|
### Rolling a number of dice and adding a bonus
|
22
50
|
```ruby
|
23
51
|
# roll a single d4
|
24
52
|
NerdDice.total_dice(4) # => return random Integer between 1-4
|
25
53
|
|
26
54
|
# roll 3d6
|
27
|
-
NerdDice.total_dice(6, 3) => return Integer total of three 6-sided dice
|
55
|
+
NerdDice.total_dice(6, 3) # => return Integer total of three 6-sided dice
|
28
56
|
|
29
57
|
# roll a d20 and add 5 to the value
|
30
|
-
NerdDice.total_dice(20,
|
58
|
+
NerdDice.total_dice(20, bonus: 5)
|
59
|
+
|
60
|
+
# roll a d20 and overide the configured randomization_technique one time
|
61
|
+
# without changing the config
|
62
|
+
NerdDice.total_dice(20, randomization_technique: :randomized)
|
63
|
+
```
|
64
|
+
__NOTE:__ If provided, the bonus must respond to `:to_i` or an `ArgumentError` will be raised
|
65
|
+
|
66
|
+
### Manually setting or refreshing the random generator seed
|
67
|
+
For randomization techniques other than `:securerandom` you can manually set or refresh the generator's seed by calling the `refresh_seed!` method. This is automatically called at the interval specified in `NerdDice.configuration.refresh_seed_interval` if it is not nil.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# no arguments, will refresh the seed for the configured generator(s) only
|
71
|
+
NerdDice.refresh_seed! # => hash with old seed(s) or nil if :securerandom
|
72
|
+
|
73
|
+
# OPTIONS:
|
74
|
+
# randomization_technique (Symbol) => NerdDice::RANDOMIZATION_TECHNIQUES
|
75
|
+
# random_rand_seed (Integer) => Seed to set for Random
|
76
|
+
# random_object_seed (Integer) => Seed to set for new Random object
|
77
|
+
NerdDice.refresh_seed!(randomization_technique: :randomized,
|
78
|
+
random_rand_seed: 1337,
|
79
|
+
random_object_seed: 24601)
|
31
80
|
```
|
32
|
-
__NOTE:__
|
81
|
+
__NOTE:__ Ability to specify a seed it primarily provided for testing purposes. This makes all random numbers generated _transparently deterministic_ and should not be used if you want behavior approximating randomness.
|
33
82
|
|
34
83
|
## Development
|
35
84
|
|
@@ -43,4 +92,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/statel
|
|
43
92
|
|
44
93
|
## Unlicense, License, and Copyright
|
45
94
|
|
46
|
-
The document is dual-licensed under the [MIT](https://opensource.org/licenses/MIT) license and the [UNLICENSE](https://unlicense.org/) \(with strong preference toward the UNLICENSE\)\. The content is released under [CC0](https://creativecommons.org/share-your-work/public-domain/cc0/) \(no rights reserved\). You are free to include it in its original form or modified with or without modification in your own project\.
|
95
|
+
The document is dual-licensed under the [MIT](https://opensource.org/licenses/MIT) license and the [UNLICENSE](https://unlicense.org/) \(with strong preference toward the UNLICENSE\)\. The content is released under [CC0](https://creativecommons.org/share-your-work/public-domain/cc0/) \(no rights reserved\). You are free to include it in its original form or modified with or without modification in your own project\.
|
@@ -0,0 +1,108 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "benchmark"
|
6
|
+
require "nerd_dice"
|
7
|
+
|
8
|
+
n = 50_000
|
9
|
+
|
10
|
+
RATIOS = {
|
11
|
+
nerd_dice_securerandom: 1.7,
|
12
|
+
nerd_dice_random_rand: 11.1,
|
13
|
+
nerd_dice_random_object: 13.0,
|
14
|
+
nerd_dice_randomized: 5.5,
|
15
|
+
nerd_dice_securerandom_3d6: 5.5,
|
16
|
+
nerd_dice_random_rand_3d6: 25.0,
|
17
|
+
nerd_dice_random_object_3d6: 25.5,
|
18
|
+
nerd_dice_randomized_3d6: 14.5
|
19
|
+
}.freeze
|
20
|
+
|
21
|
+
def check_against_baseline!(baseline_value, test_value)
|
22
|
+
ratio = RATIOS[test_value.label.to_sym]
|
23
|
+
error_message = "Failed benchmark for #{test_value.label}. "
|
24
|
+
error_message += "Allowed ratio was #{ratio} actual ratio was #{test_value.real / baseline_value}"
|
25
|
+
raise NerdDice::Error, error_message if test_value.real > baseline_value * ratio
|
26
|
+
end
|
27
|
+
|
28
|
+
puts "Set baseline"
|
29
|
+
baselines = Benchmark.bmbm do |x|
|
30
|
+
# Random.rand()
|
31
|
+
x.report("Random.rand") do # standard rand()
|
32
|
+
n.times { Random.rand(1000) }
|
33
|
+
end
|
34
|
+
|
35
|
+
# SecureRandom.rand()
|
36
|
+
x.report("Sec.rand") do
|
37
|
+
n.times { SecureRandom.rand(1000) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
random_rand_baseline = baselines[0].real
|
42
|
+
securerandom_baseline = baselines[1].real
|
43
|
+
|
44
|
+
puts "Roll d1000s"
|
45
|
+
total_dice_d1000_results = Benchmark.bmbm do |x|
|
46
|
+
# NerdDice.total_dice securerandom
|
47
|
+
x.report("nerd_dice_securerandom") do
|
48
|
+
NerdDice.configuration.randomization_technique = :securerandom
|
49
|
+
n.times { NerdDice.total_dice(1000) }
|
50
|
+
end
|
51
|
+
|
52
|
+
x.report("nerd_dice_random_rand") do
|
53
|
+
NerdDice.configuration.randomization_technique = :random_rand
|
54
|
+
n.times { NerdDice.total_dice(1000) }
|
55
|
+
end
|
56
|
+
|
57
|
+
x.report("nerd_dice_random_object") do
|
58
|
+
NerdDice.configuration.randomization_technique = :random_object
|
59
|
+
n.times { NerdDice.total_dice(1000) }
|
60
|
+
end
|
61
|
+
|
62
|
+
x.report("nerd_dice_randomized") do
|
63
|
+
NerdDice.configuration.randomization_technique = :randomized
|
64
|
+
n.times { NerdDice.total_dice(1000) }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
total_dice_securerandom = total_dice_d1000_results[0]
|
69
|
+
check_against_baseline! securerandom_baseline, total_dice_securerandom
|
70
|
+
total_dice_random_rand = total_dice_d1000_results[1]
|
71
|
+
check_against_baseline! random_rand_baseline, total_dice_random_rand
|
72
|
+
total_dice_random_object = total_dice_d1000_results[2]
|
73
|
+
check_against_baseline! random_rand_baseline, total_dice_random_object
|
74
|
+
total_dice_randomized = total_dice_d1000_results[3]
|
75
|
+
check_against_baseline! ((random_rand_baseline * 0.75) + (securerandom_baseline * 0.25)), total_dice_randomized
|
76
|
+
|
77
|
+
puts "Roll 3d6"
|
78
|
+
total_dice_3d6_results = Benchmark.bmbm do |x|
|
79
|
+
# NerdDice.total_dice securerandom
|
80
|
+
x.report("nerd_dice_securerandom_3d6") do
|
81
|
+
NerdDice.configuration.randomization_technique = :securerandom
|
82
|
+
n.times { NerdDice.total_dice(6, 3) }
|
83
|
+
end
|
84
|
+
|
85
|
+
x.report("nerd_dice_random_rand_3d6") do
|
86
|
+
NerdDice.configuration.randomization_technique = :random_rand
|
87
|
+
n.times { NerdDice.total_dice(6, 3) }
|
88
|
+
end
|
89
|
+
|
90
|
+
x.report("nerd_dice_random_object_3d6") do
|
91
|
+
NerdDice.configuration.randomization_technique = :random_object
|
92
|
+
n.times { NerdDice.total_dice(6, 3) }
|
93
|
+
end
|
94
|
+
|
95
|
+
x.report("nerd_dice_randomized_3d6") do
|
96
|
+
NerdDice.configuration.randomization_technique = :randomized
|
97
|
+
n.times { NerdDice.total_dice(6, 3) }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
total_dice_3d6_securerandom = total_dice_3d6_results[0]
|
102
|
+
check_against_baseline! securerandom_baseline, total_dice_3d6_securerandom
|
103
|
+
total_dice_3d6_random_rand = total_dice_3d6_results[1]
|
104
|
+
check_against_baseline! random_rand_baseline, total_dice_3d6_random_rand
|
105
|
+
total_dice_3d6_random_object = total_dice_3d6_results[2]
|
106
|
+
check_against_baseline! random_rand_baseline, total_dice_3d6_random_object
|
107
|
+
total_dice_3d6_randomized = total_dice_3d6_results[3]
|
108
|
+
check_against_baseline! ((random_rand_baseline * 0.75) + (securerandom_baseline * 0.25)), total_dice_3d6_randomized
|
@@ -0,0 +1 @@
|
|
1
|
+
0a2d4765e24c21fecf99229b0cc4fad806ca256d2187c45c442b25771af1b9cc
|
@@ -0,0 +1 @@
|
|
1
|
+
e345190f870eabd2a4fd6993f1a1d70d008ef8e6464767ba5697a46f7c4d6912c78a18039cec06c18dd6f0ee9f425453c078f780d36ccf65043ec3f7fba790a2
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NerdDice
|
4
|
+
# The NerdDice::Configuration class allows you to configure and customize the
|
5
|
+
# options of the NerdDice gem to suit your specific needs. You can specify
|
6
|
+
# properties like the randomization technique used by the gem, the number of
|
7
|
+
# ability scores in an ability score array, etc. See the README for a list of
|
8
|
+
# configurable attributes.
|
9
|
+
#
|
10
|
+
# Usage:
|
11
|
+
# The configuration can either be set via a configure block:
|
12
|
+
# <tt>NerdDice.configure do |config|
|
13
|
+
# config.randomization_technique = :random_new_interval
|
14
|
+
# config.new_random_interval = 100
|
15
|
+
# end
|
16
|
+
# </tt>
|
17
|
+
#
|
18
|
+
# You can also set a particular property without a block using inline assignment
|
19
|
+
# <tt>NerdDice.configuration.randomization_technique = :random_new_once</tt>
|
20
|
+
class Configuration
|
21
|
+
attr_reader :randomization_technique, :refresh_seed_interval
|
22
|
+
attr_accessor :ability_score_array_size
|
23
|
+
|
24
|
+
def randomization_technique=(value)
|
25
|
+
unless RANDOMIZATION_TECHNIQUES.include?(value)
|
26
|
+
raise NerdDice::Error, "randomization_technique must be one of #{RANDOMIZATION_TECHNIQUES.join(', ')}"
|
27
|
+
end
|
28
|
+
|
29
|
+
@randomization_technique = value
|
30
|
+
end
|
31
|
+
|
32
|
+
def refresh_seed_interval=(value)
|
33
|
+
unless value.nil?
|
34
|
+
value = value&.to_i
|
35
|
+
raise NerdDice::Error, "refresh_seed_interval must be a positive integer or nil" unless value.positive?
|
36
|
+
end
|
37
|
+
@refresh_seed_interval = value
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def initialize
|
43
|
+
@ability_score_array_size = 6
|
44
|
+
@randomization_technique = :random_object
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/nerd_dice/version.rb
CHANGED
data/lib/nerd_dice.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "nerd_dice/version"
|
4
|
+
require "nerd_dice/configuration"
|
5
|
+
require "securerandom"
|
4
6
|
# Nerd dice allows you to roll polyhedral dice and add bonuses as you would in
|
5
7
|
# a tabletop roleplaying game. You can choose to roll multiple dice and keep a
|
6
8
|
# specified number of dice such as rolling 4d6 and dropping the lowest for
|
@@ -23,25 +25,152 @@ require "nerd_dice/version"
|
|
23
25
|
module NerdDice
|
24
26
|
class Error < StandardError; end
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
28
|
+
RANDOMIZATION_TECHNIQUES = %i[securerandom random_rand random_object randomized].freeze
|
29
|
+
|
30
|
+
class << self
|
31
|
+
attr_reader :count_since_last_refresh
|
32
|
+
|
33
|
+
############################
|
34
|
+
# configure class method
|
35
|
+
############################
|
36
|
+
# Arguments: None
|
37
|
+
# Expects and yields to a block where configuration is specified.
|
38
|
+
# See README and NerdDice::Configuration class for config options
|
39
|
+
# Return (NerdDice::Configuration) the Configuration object tied to the
|
40
|
+
# @configuration class instance variable
|
41
|
+
def configure
|
42
|
+
yield configuration
|
43
|
+
configuration
|
44
|
+
end
|
45
|
+
|
46
|
+
############################
|
47
|
+
# configuration class method
|
48
|
+
############################
|
49
|
+
# Arguments: None
|
50
|
+
# Provides the lazy-loaded class instance variable @configuration
|
51
|
+
# Return (NerdDice::Configuration) the Configuration object tied to the
|
52
|
+
# @configuration class instance variable
|
53
|
+
def configuration
|
54
|
+
@configuration ||= Configuration.new
|
55
|
+
end
|
56
|
+
|
57
|
+
############################
|
58
|
+
# total_dice class method
|
59
|
+
############################
|
60
|
+
# Arguments:
|
61
|
+
# number_of_sides (Integer) => the number of sides of the dice to roll
|
62
|
+
# number_of_dice (Integer, DEFAULT: 1) => the quantity to roll of the type
|
63
|
+
# of die specified in the number_of_sides argument.
|
64
|
+
# options (Hash, DEFAULT: {}) any additional options you wish to include
|
65
|
+
# :bonus (Integer) => The total bonus (positive integer) or penalty
|
66
|
+
# (negative integer) to modify the total by. Is added to the total of
|
67
|
+
# all dice after they are totaled, not to each die rolled
|
68
|
+
#
|
69
|
+
# Return (Integer) => Total of the dice rolled, plus modifier if applicable
|
70
|
+
def total_dice(number_of_sides, number_of_dice = 1, **opts)
|
71
|
+
total = 0
|
72
|
+
number_of_dice.times do
|
73
|
+
total += execute_die_roll(number_of_sides, opts[:randomization_technique])
|
74
|
+
end
|
75
|
+
begin
|
76
|
+
total += opts[:bonus].to_i
|
77
|
+
rescue NoMethodError
|
78
|
+
raise ArgumentError, "Bonus must be a value that responds to :to_i"
|
79
|
+
end
|
80
|
+
total
|
81
|
+
end
|
82
|
+
|
83
|
+
############################
|
84
|
+
# execute_die_roll class method
|
85
|
+
############################
|
86
|
+
# Arguments:
|
87
|
+
# number_of_sides (Integer) => the number of sides of the die to roll
|
88
|
+
# using_generator (Symbol) => must be one of the symbols in
|
89
|
+
# RANDOMIZATION_TECHNIQUES or nil
|
90
|
+
#
|
91
|
+
# Return (Integer) => Value of the single die rolled
|
92
|
+
def execute_die_roll(number_of_sides, using_generator = nil)
|
93
|
+
@count_since_last_refresh ||= 0
|
94
|
+
gen = get_number_generator(using_generator)
|
95
|
+
result = gen.rand(number_of_sides) + 1
|
96
|
+
increment_and_evalutate_refresh_seed
|
97
|
+
result
|
98
|
+
end
|
99
|
+
|
100
|
+
############################
|
101
|
+
# refresh_seed! class method
|
102
|
+
############################
|
103
|
+
# Options: (none required)
|
104
|
+
# randomization_technique (Symbol) => must be one of the symbols in
|
105
|
+
# RANDOMIZATION_TECHNIQUES if specified
|
106
|
+
# random_rand_seed (Integer) => Seed to set for Random
|
107
|
+
# random_object_seed (Integer) => Seed to set for new Random object
|
108
|
+
# Return (Hash or nil) => Previous values of generator seeds that were refreshed
|
109
|
+
def refresh_seed!(**opts)
|
110
|
+
technique, random_rand_new_seed, random_object_new_seed = parse_refresh_options(opts)
|
111
|
+
@count_since_last_refresh = 0
|
112
|
+
return nil if technique == :securerandom
|
113
|
+
|
114
|
+
reset_appropriate_seeds!(technique, random_rand_new_seed, random_object_new_seed)
|
43
115
|
end
|
44
|
-
|
45
|
-
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def get_number_generator(using_generator = nil)
|
120
|
+
using_generator ||= configuration.randomization_technique
|
121
|
+
case using_generator
|
122
|
+
when :securerandom then SecureRandom
|
123
|
+
when :random_rand then Random
|
124
|
+
when :random_object then @random_object ||= Random.new
|
125
|
+
when :randomized then random_generator
|
126
|
+
else raise ArgumentError, "Unrecognized generator. Must be one of #{RANDOMIZATION_TECHNIQUES.join(', ')}"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def random_generator
|
131
|
+
gen = RANDOMIZATION_TECHNIQUES.reject { |el| el == :randomized }.sample
|
132
|
+
get_number_generator(gen)
|
133
|
+
end
|
134
|
+
|
135
|
+
def refresh_random_rand_seed!(new_seed)
|
136
|
+
new_seed ? Random.srand(new_seed) : Random.srand
|
137
|
+
end
|
138
|
+
|
139
|
+
def refresh_random_object_seed!(new_seed)
|
140
|
+
old_seed = @random_object&.seed
|
141
|
+
@random_object = new_seed ? Random.new(new_seed) : Random.new
|
142
|
+
old_seed
|
143
|
+
end
|
144
|
+
|
145
|
+
def parse_refresh_options(opts)
|
146
|
+
[
|
147
|
+
opts[:randomization_technique] || configuration.randomization_technique,
|
148
|
+
opts[:random_rand_seed],
|
149
|
+
opts[:random_object_seed]
|
150
|
+
]
|
151
|
+
end
|
152
|
+
|
153
|
+
# rubocop:disable Metrics/MethodLength
|
154
|
+
def reset_appropriate_seeds!(technique, random_rand_new_seed, random_object_new_seed)
|
155
|
+
return_hash = {}
|
156
|
+
case technique
|
157
|
+
when :random_rand
|
158
|
+
return_hash[:random_rand_prior_seed] = refresh_random_rand_seed!(random_rand_new_seed)
|
159
|
+
when :random_object
|
160
|
+
return_hash[:random_object_prior_seed] = refresh_random_object_seed!(random_object_new_seed)
|
161
|
+
when :randomized
|
162
|
+
return_hash[:random_rand_prior_seed] = refresh_random_rand_seed!(random_rand_new_seed)
|
163
|
+
return_hash[:random_object_prior_seed] = refresh_random_object_seed!(random_object_new_seed)
|
164
|
+
end
|
165
|
+
return_hash
|
166
|
+
end
|
167
|
+
# rubocop:enable Metrics/MethodLength
|
168
|
+
|
169
|
+
def increment_and_evalutate_refresh_seed
|
170
|
+
@count_since_last_refresh += 1
|
171
|
+
return unless configuration.refresh_seed_interval
|
172
|
+
|
173
|
+
refresh_seed! if @count_since_last_refresh >= configuration.refresh_seed_interval
|
174
|
+
end
|
46
175
|
end
|
47
176
|
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nerd_dice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Duchemin
|
@@ -177,14 +177,15 @@ email:
|
|
177
177
|
executables:
|
178
178
|
- console
|
179
179
|
- generate_checksums
|
180
|
+
- nerd_dice_benchmark
|
180
181
|
- setup
|
181
182
|
extensions: []
|
182
183
|
extra_rdoc_files: []
|
183
184
|
files:
|
185
|
+
- ".github/workflows/main.yml"
|
184
186
|
- ".gitignore"
|
185
187
|
- ".rspec"
|
186
188
|
- ".rubocop.yml"
|
187
|
-
- ".travis.yml"
|
188
189
|
- BURN_THE_CONTRIBUTOR_COVENANT_WITH_FIRE.md
|
189
190
|
- CHANGELOG.md
|
190
191
|
- Gemfile
|
@@ -195,13 +196,17 @@ files:
|
|
195
196
|
- UNLICENSE.txt
|
196
197
|
- bin/console
|
197
198
|
- bin/generate_checksums
|
199
|
+
- bin/nerd_dice_benchmark
|
198
200
|
- bin/setup
|
199
201
|
- certs/msducheminjr.pem
|
200
202
|
- checksum/nerd_dice-0.1.0.gem.sha256
|
201
203
|
- checksum/nerd_dice-0.1.0.gem.sha512
|
202
204
|
- checksum/nerd_dice-0.1.1.gem.sha256
|
203
205
|
- checksum/nerd_dice-0.1.1.gem.sha512
|
206
|
+
- checksum/nerd_dice-0.2.0.gem.sha256
|
207
|
+
- checksum/nerd_dice-0.2.0.gem.sha512
|
204
208
|
- lib/nerd_dice.rb
|
209
|
+
- lib/nerd_dice/configuration.rb
|
205
210
|
- lib/nerd_dice/version.rb
|
206
211
|
- nerd_dice.gemspec
|
207
212
|
homepage: https://github.com/statelesscode/nerd_dice
|
metadata.gz.sig
CHANGED
Binary file
|