simple-random 1.0.4 → 1.0.6
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
- data/Gemfile +1 -4
- data/README.md +66 -7
- data/Rakefile +4 -34
- data/VERSION +1 -1
- data/lib/simple-random/multi_threaded_simple_random.rb +6 -19
- data/lib/simple-random/simple_random.rb +35 -2
- data/simple-random.gemspec +2 -28
- data/test/helper.rb +15 -26
- data/test/test_simple_random.rb +165 -1
- metadata +4 -64
- data/.travis.yml +0 -22
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ebb6c9fcca2c979b9311eef572e914e208132f58189ee8ef99206b2a99a0aa7d
|
|
4
|
+
data.tar.gz: 9bfe40555eb0cc5c25453b92754151ff715c719fa393f9da598592df7df24f8b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4721b67c356ccb83d574c66fd9753ac689c4011d4864248e81b01dfcdf3b04cfe05ffa891f9a5e668c672abc9d05387757fc5d182efda9f5ba0fed3298b10013
|
|
7
|
+
data.tar.gz: 86edd392eef5806ac0cd09aad2085efe5ff690cb27cabd2b9a3bb2dfe0d344e4f991ee8b585a8c0e1eb14f36106fa61d9bcbd4719e75a52732d7172d78701e4f
|
data/Gemfile
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
source 'https://rubygems.org'
|
|
2
2
|
|
|
3
3
|
group :development do
|
|
4
|
-
gem 'awesome_print', '>= 1.6'
|
|
5
4
|
gem 'minitest'
|
|
6
5
|
gem 'shoulda'
|
|
7
|
-
gem '
|
|
6
|
+
gem 'rake'
|
|
8
7
|
gem 'simplecov'
|
|
9
|
-
gem 'nokogiri', '>= 1.8.5'
|
|
10
|
-
gem 'codeclimate-test-reporter', require: nil
|
|
11
8
|
end
|
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# simple-random ](https://github.com/ealdent/simple-random/actions/workflows/ci.yml)
|
|
2
2
|
|
|
3
3
|
Generate random numbers sampled from the following distributions:
|
|
4
4
|
|
|
@@ -18,6 +18,27 @@ Generate random numbers sampled from the following distributions:
|
|
|
18
18
|
|
|
19
19
|
Based on [John D. Cook's SimpleRNG](http://www.codeproject.com/KB/recipes/SimpleRNG.aspx) C# library.
|
|
20
20
|
|
|
21
|
+
## Security
|
|
22
|
+
|
|
23
|
+
**Do not use `SimpleRandom` or `MultiThreadedSimpleRandom` for security-sensitive randomness.**
|
|
24
|
+
They are deterministic generators for statistical sampling, not cryptographically secure
|
|
25
|
+
random number generators. The default seeds are fixed, and calling `set_seed` without
|
|
26
|
+
arguments uses the current time. Changing the seed does not make the generator secure.
|
|
27
|
+
|
|
28
|
+
For passwords, password-reset tokens, session tokens, encryption keys, or other values
|
|
29
|
+
that must be unpredictable, use Ruby's
|
|
30
|
+
[`SecureRandom`](https://docs.ruby-lang.org/en/3.4/SecureRandom.html):
|
|
31
|
+
|
|
32
|
+
``` ruby
|
|
33
|
+
require 'securerandom'
|
|
34
|
+
|
|
35
|
+
# 32 random bytes, encoded as 64 hexadecimal characters
|
|
36
|
+
token = SecureRandom.hex(32)
|
|
37
|
+
|
|
38
|
+
# An integer from 0 through 99
|
|
39
|
+
number = SecureRandom.random_number(100)
|
|
40
|
+
```
|
|
41
|
+
|
|
21
42
|
## Installation
|
|
22
43
|
|
|
23
44
|
### Plain Ruby
|
|
@@ -52,16 +73,44 @@ Note that by default the same seed is used every time to generate the random num
|
|
|
52
73
|
See [lib/simple-random.rb](lib/simple-random/simple_random.rb) for all available methods and options.
|
|
53
74
|
|
|
54
75
|
|
|
55
|
-
##
|
|
76
|
+
## Development
|
|
77
|
+
|
|
78
|
+
Install the development dependencies with Bundler:
|
|
79
|
+
|
|
80
|
+
``` sh
|
|
81
|
+
bundle install
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Run the test suite:
|
|
85
|
+
|
|
86
|
+
``` sh
|
|
87
|
+
bundle exec rake test
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Build the gem locally:
|
|
91
|
+
|
|
92
|
+
``` sh
|
|
93
|
+
bundle exec rake build
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Generate local coverage output when needed:
|
|
97
|
+
|
|
98
|
+
``` sh
|
|
99
|
+
bundle exec rake simplecov
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
GitHub Actions runs the test suite and gem build on Ruby 2.6 and Ruby 3.0 through 3.4.
|
|
103
|
+
|
|
104
|
+
## Contributing
|
|
56
105
|
|
|
57
106
|
* Fork the project.
|
|
58
107
|
* Make your feature addition or bug fix.
|
|
59
|
-
* Add tests for
|
|
60
|
-
*
|
|
61
|
-
|
|
62
|
-
* Send
|
|
108
|
+
* Add or update tests for behavioral changes.
|
|
109
|
+
* Run `bundle exec rake test` before opening a pull request.
|
|
110
|
+
* Keep release-only files such as `VERSION` and `simple-random.gemspec` unchanged unless the pull request is specifically preparing a release.
|
|
111
|
+
* Send a pull request.
|
|
63
112
|
|
|
64
|
-
|
|
113
|
+
The project uses plain Bundler, Rake, and GitHub Actions. It no longer uses Jeweler, Travis CI, or Code Climate.
|
|
65
114
|
|
|
66
115
|
## Copyright
|
|
67
116
|
|
|
@@ -69,8 +118,18 @@ Distributed under the Code Project Open License, which is similar to MIT or BSD.
|
|
|
69
118
|
|
|
70
119
|
## History
|
|
71
120
|
|
|
121
|
+
### 1.0.6 - 2026-11-15
|
|
122
|
+
* Prevent gamma sampling hangs for non-finite shapes and fixed-point seeds
|
|
123
|
+
* Keep uniform samples inside their documented open interval, including narrow and very wide bounds; reject intervals with no representable interior float
|
|
124
|
+
* Allow generators from finished threads to be garbage collected while preserving sharing across fibers within a thread
|
|
125
|
+
* Document that these generators are unsuitable for security-sensitive randomness and recommend Ruby's `SecureRandom`
|
|
126
|
+
|
|
127
|
+
### 1.0.5 - 2026-05-20
|
|
128
|
+
* Remove development dependency metadata from the published gemspec
|
|
129
|
+
|
|
72
130
|
### 1.0.4 - 2026-02-18
|
|
73
131
|
* Remove vulnerable development dependency declarations for `bundler` and `rdoc`
|
|
132
|
+
* Replace obsolete Jeweler/RDoc release tasks with a plain gem build task
|
|
74
133
|
* Use `https://rubygems.org` as the gem source in development
|
|
75
134
|
|
|
76
135
|
### 1.0.3 - 2015-11-25
|
data/Rakefile
CHANGED
|
@@ -1,31 +1,11 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
|
|
3
|
-
require 'rubygems'
|
|
4
|
-
require 'bundler'
|
|
5
|
-
begin
|
|
6
|
-
Bundler.setup(:default, :development)
|
|
7
|
-
rescue Bundler::BundlerError => e
|
|
8
|
-
$stderr.puts e.message
|
|
9
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
|
10
|
-
exit e.status_code
|
|
11
|
-
end
|
|
12
1
|
require 'rake'
|
|
2
|
+
require 'rake/testtask'
|
|
13
3
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
gem.name = "simple-random"
|
|
18
|
-
gem.homepage = "http://github.com/ealdent/simple-random"
|
|
19
|
-
gem.licenses = "CDDL-1.0"
|
|
20
|
-
gem.summary = %Q{Simple Random Number Generator}
|
|
21
|
-
gem.description = %Q{Simple Random Number Generator including Beta, Cauchy, Chi square, Exponential, Gamma, Inverse Gamma, Laplace (double exponential), Normal, Student t, Uniform, and Weibull. Ported from John D. Cook's C# Code.}
|
|
22
|
-
gem.email = "jasonmadams@gmail.com"
|
|
23
|
-
gem.authors = ["John D. Cook", "Jason Adams"]
|
|
24
|
-
# dependencies defined in Gemfile
|
|
4
|
+
desc "Build the gem package"
|
|
5
|
+
task :build do
|
|
6
|
+
sh "gem build simple-random.gemspec"
|
|
25
7
|
end
|
|
26
|
-
Jeweler::RubygemsDotOrgTasks.new
|
|
27
8
|
|
|
28
|
-
require 'rake/testtask'
|
|
29
9
|
Rake::TestTask.new(:test) do |test|
|
|
30
10
|
test.libs << 'lib' << 'test'
|
|
31
11
|
test.pattern = 'test/**/test_*.rb'
|
|
@@ -39,13 +19,3 @@ task :simplecov do
|
|
|
39
19
|
end
|
|
40
20
|
|
|
41
21
|
task :default => :test
|
|
42
|
-
|
|
43
|
-
require 'rdoc/task'
|
|
44
|
-
Rake::RDocTask.new do |rdoc|
|
|
45
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
|
46
|
-
|
|
47
|
-
rdoc.rdoc_dir = 'rdoc'
|
|
48
|
-
rdoc.title = "simple-random #{version}"
|
|
49
|
-
rdoc.rdoc_files.include('README*')
|
|
50
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
51
|
-
end
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.0.
|
|
1
|
+
1.0.6
|
|
@@ -1,27 +1,14 @@
|
|
|
1
|
-
require 'monitor'
|
|
2
|
-
|
|
3
1
|
class MultiThreadedSimpleRandom < SimpleRandom
|
|
4
2
|
class << self
|
|
5
|
-
@instances = nil
|
|
6
|
-
|
|
7
3
|
def instance
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
instance_id = Thread.current.object_id
|
|
17
|
-
|
|
18
|
-
unless @instances[instance_id]
|
|
19
|
-
self.synchronize do
|
|
20
|
-
@instances[instance_id] ||= new
|
|
21
|
-
end
|
|
4
|
+
# Thread variables are shared by fibers and collected with their owning thread.
|
|
5
|
+
instances = Thread.current.thread_variable_get(:simple_random_instances)
|
|
6
|
+
unless instances
|
|
7
|
+
instances = {}
|
|
8
|
+
Thread.current.thread_variable_set(:simple_random_instances, instances)
|
|
22
9
|
end
|
|
23
10
|
|
|
24
|
-
|
|
11
|
+
instances[self] ||= new
|
|
25
12
|
end
|
|
26
13
|
end
|
|
27
14
|
|
|
@@ -30,10 +30,41 @@ class SimpleRandom
|
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
# Produce a uniform random sample from the open interval (lower, upper).
|
|
33
|
+
# Raise ArgumentError if the bounds contain no representable interior float.
|
|
33
34
|
def uniform(lower = 0, upper = 1)
|
|
34
35
|
fail ArgumentError, 'Upper bound must be greater than lower bound.' unless lower < upper
|
|
35
36
|
|
|
36
|
-
|
|
37
|
+
sample = get_unsigned_int + 1
|
|
38
|
+
# Keep the largest integer below the upper bound without changing other samples.
|
|
39
|
+
sample -= 0.5 if sample == I_32_BIT
|
|
40
|
+
|
|
41
|
+
value = (sample * (upper - lower) / F_32_BIT) + lower
|
|
42
|
+
|
|
43
|
+
# Recompute overflowed intermediates without changing finite seeded results.
|
|
44
|
+
if !value.finite? && lower.to_f.finite? && upper.to_f.finite?
|
|
45
|
+
fraction = sample / F_32_BIT
|
|
46
|
+
value = if lower < 0 && upper > 0
|
|
47
|
+
# Opposite-sign bounds can overflow when subtracted.
|
|
48
|
+
(1.0 - fraction) * lower + fraction * upper
|
|
49
|
+
else
|
|
50
|
+
lower + fraction * (upper - lower)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Scaling and translation can round a sample back onto an excluded endpoint.
|
|
55
|
+
if value <= lower
|
|
56
|
+
value = lower.to_f
|
|
57
|
+
value = value.next_float if value <= lower
|
|
58
|
+
elsif value >= upper
|
|
59
|
+
value = upper.to_f
|
|
60
|
+
value = value.prev_float if value >= upper
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
unless lower < value && value < upper
|
|
64
|
+
fail ArgumentError, 'Bounds must contain a representable interior float.'
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
value
|
|
37
68
|
end
|
|
38
69
|
|
|
39
70
|
# Sample normal distribution with given mean and standard deviation
|
|
@@ -69,7 +100,9 @@ class SimpleRandom
|
|
|
69
100
|
# by George Marsaglia and Wai Wan Tsang. ACM Transactions on Mathematical Software
|
|
70
101
|
# Vol 26, No 3, September 2000, pages 363-372.
|
|
71
102
|
def gamma(shape, scale)
|
|
72
|
-
|
|
103
|
+
unless shape > 0 && shape.to_f.finite?
|
|
104
|
+
fail ArgumentError, 'Shape must be finite and strictly positive'
|
|
105
|
+
end
|
|
73
106
|
return scale * gamma(shape + 1.0, 1.0) * uniform ** -shape if shape < 1
|
|
74
107
|
|
|
75
108
|
d = shape - 1 / 3.0
|
data/simple-random.gemspec
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
# Generated by jeweler
|
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
|
4
1
|
# -*- encoding: utf-8 -*-
|
|
5
|
-
# stub: simple-random 1.0.1 ruby lib
|
|
6
2
|
|
|
7
3
|
Gem::Specification.new do |s|
|
|
8
4
|
s.name = "simple-random"
|
|
9
|
-
s.version = "1.0.
|
|
5
|
+
s.version = "1.0.6"
|
|
10
6
|
|
|
11
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
12
8
|
s.require_paths = ["lib"]
|
|
13
9
|
s.authors = ["John D. Cook", "Jason Adams"]
|
|
14
|
-
s.date = "2026-
|
|
10
|
+
s.date = "2026-11-15"
|
|
15
11
|
s.description = "Simple Random Number Generator including Beta, Cauchy, Chi square, Exponential, Gamma, Inverse Gamma, Laplace (double exponential), Normal, Student t, Uniform, and Weibull. Ported from John D. Cook's C# Code."
|
|
16
12
|
s.email = "jasonmadams@gmail.com"
|
|
17
13
|
s.extra_rdoc_files = [
|
|
@@ -20,7 +16,6 @@ Gem::Specification.new do |s|
|
|
|
20
16
|
]
|
|
21
17
|
s.files = [
|
|
22
18
|
".document",
|
|
23
|
-
".travis.yml",
|
|
24
19
|
"Gemfile",
|
|
25
20
|
"LICENSE",
|
|
26
21
|
"README.md",
|
|
@@ -35,27 +30,6 @@ Gem::Specification.new do |s|
|
|
|
35
30
|
]
|
|
36
31
|
s.homepage = "http://github.com/ealdent/simple-random"
|
|
37
32
|
s.licenses = ["CDDL-1.0"]
|
|
38
|
-
s.rubygems_version = "2.2.2"
|
|
39
33
|
s.summary = "Simple Random Number Generator"
|
|
40
34
|
|
|
41
|
-
if s.respond_to? :specification_version then
|
|
42
|
-
s.specification_version = 4
|
|
43
|
-
|
|
44
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
45
|
-
s.add_development_dependency(%q<minitest>, [">= 0"])
|
|
46
|
-
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
|
47
|
-
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
|
48
|
-
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
|
49
|
-
else
|
|
50
|
-
s.add_dependency(%q<minitest>, [">= 0"])
|
|
51
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
|
52
|
-
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
|
53
|
-
s.add_dependency(%q<simplecov>, [">= 0"])
|
|
54
|
-
end
|
|
55
|
-
else
|
|
56
|
-
s.add_dependency(%q<minitest>, [">= 0"])
|
|
57
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
|
58
|
-
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
|
59
|
-
s.add_dependency(%q<simplecov>, [">= 0"])
|
|
60
|
-
end
|
|
61
35
|
end
|
data/test/helper.rb
CHANGED
|
@@ -1,37 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
require '
|
|
1
|
+
if ENV["COVERAGE"]
|
|
2
|
+
require 'simplecov'
|
|
3
3
|
|
|
4
|
-
module SimpleCov::Configuration
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
module SimpleCov::Configuration
|
|
5
|
+
def clean_filters
|
|
6
|
+
@filters = []
|
|
7
|
+
end
|
|
7
8
|
end
|
|
8
|
-
end
|
|
9
9
|
|
|
10
|
-
SimpleCov.configure do
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
end
|
|
10
|
+
SimpleCov.configure do
|
|
11
|
+
clean_filters
|
|
12
|
+
load_profile 'test_frameworks'
|
|
13
|
+
end
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
end
|
|
18
|
-
require 'rubygems'
|
|
19
|
-
require 'bundler'
|
|
20
|
-
begin
|
|
21
|
-
Bundler.setup(:default, :development)
|
|
22
|
-
rescue Bundler::BundlerError => e
|
|
23
|
-
$stderr.puts e.message
|
|
24
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
|
25
|
-
exit e.status_code
|
|
15
|
+
SimpleCov.start do
|
|
16
|
+
add_filter "/.rvm/"
|
|
17
|
+
end
|
|
26
18
|
end
|
|
19
|
+
|
|
20
|
+
require 'time'
|
|
27
21
|
require 'minitest/autorun'
|
|
28
22
|
require 'shoulda'
|
|
29
23
|
|
|
30
24
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
31
25
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
32
26
|
require 'simple-random'
|
|
33
|
-
|
|
34
|
-
class MiniTest::Test
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
MiniTest.autorun
|
data/test/test_simple_random.rb
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
require 'helper'
|
|
2
|
+
require 'timeout'
|
|
3
|
+
require 'weakref'
|
|
2
4
|
|
|
3
5
|
# TODO: use Kolmogorov-Smirnov test instead: http://en.wikipedia.org/wiki/Kolmogorov_Smirnov
|
|
4
6
|
|
|
@@ -32,7 +34,7 @@ def Time.now
|
|
|
32
34
|
new(2015, 11, 26, 12, 1, 15, '-05:00')
|
|
33
35
|
end
|
|
34
36
|
|
|
35
|
-
class TestSimpleRandom <
|
|
37
|
+
class TestSimpleRandom < Minitest::Test
|
|
36
38
|
context "Setting the seeds for a simple random number generator" do
|
|
37
39
|
context "on initialization" do
|
|
38
40
|
should "assign default seeds when none are specified" do
|
|
@@ -104,6 +106,109 @@ class TestSimpleRandom < MiniTest::Test
|
|
|
104
106
|
end
|
|
105
107
|
end
|
|
106
108
|
|
|
109
|
+
should "exclude the upper bound even with fixed-point seeds" do
|
|
110
|
+
r = SimpleRandom.new(1179647999, 2422800383)
|
|
111
|
+
|
|
112
|
+
3.times do
|
|
113
|
+
assert_operator r.uniform, :>, 0
|
|
114
|
+
assert_operator r.uniform, :<, 1
|
|
115
|
+
assert_operator r.uniform(-5, 5), :<, 5
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
should "preserve seeded samples away from the upper endpoint" do
|
|
120
|
+
expected = [0.12706408719532192, 0.46806520386599004, 0.9058290126267821]
|
|
121
|
+
assert_equal expected, Array.new(3) { @r.uniform }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
should "preserve finite seeded samples with nondefault bounds" do
|
|
125
|
+
expected = [0.12541281743906438, 0.19361304077319802, 0.2811658025253564]
|
|
126
|
+
assert_equal expected, Array.new(3) { @r.uniform(0.1, 0.3) }
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
should "preserve distinct draws across wide finite intervals" do
|
|
130
|
+
[
|
|
131
|
+
[0.0, 1e308],
|
|
132
|
+
[-1e308, 0.0],
|
|
133
|
+
[-Float::MAX, Float::MAX],
|
|
134
|
+
[Float::MAX / 2, Float::MAX],
|
|
135
|
+
[-Float::MAX, -Float::MAX / 2]
|
|
136
|
+
].each do |lower, upper|
|
|
137
|
+
generator = SimpleRandom.new
|
|
138
|
+
reference = SimpleRandom.new
|
|
139
|
+
|
|
140
|
+
samples = Array.new(3) do
|
|
141
|
+
sample = generator.uniform(lower, upper)
|
|
142
|
+
# Exact rational arithmetic supplies an independent interpolation reference.
|
|
143
|
+
expected = (lower.to_r + reference.uniform.to_r * (upper.to_r - lower.to_r)).to_f
|
|
144
|
+
|
|
145
|
+
assert sample.finite?
|
|
146
|
+
assert_operator sample, :>, lower
|
|
147
|
+
assert_operator sample, :<, upper
|
|
148
|
+
assert_in_delta expected, sample, expected.abs * 1e-14
|
|
149
|
+
sample
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
assert_equal 3, samples.uniq.size
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
should "preserve extreme seeded draws across the widest finite interval" do
|
|
157
|
+
samples = [
|
|
158
|
+
SimpleRandom.new(1179647999, 2422800383),
|
|
159
|
+
SimpleRandom.new(4294967296, 4294967296)
|
|
160
|
+
].map do |generator|
|
|
161
|
+
sample = generator.uniform(-Float::MAX, Float::MAX)
|
|
162
|
+
|
|
163
|
+
assert sample.finite?
|
|
164
|
+
assert_operator sample, :>, -Float::MAX
|
|
165
|
+
assert_operator sample, :<, Float::MAX
|
|
166
|
+
sample
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
assert_operator samples.first, :>, 0
|
|
170
|
+
assert_operator samples.last, :<, 0
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
should "exclude rounded endpoints for narrow bounds with an offset" do
|
|
174
|
+
generators = [
|
|
175
|
+
SimpleRandom.new(1179647999, 2422800383),
|
|
176
|
+
SimpleRandom.new(4294967296, 4294967296)
|
|
177
|
+
]
|
|
178
|
+
|
|
179
|
+
[[1.0, 1.00000001], [-1.00000001, -1.0]].each do |lower, upper|
|
|
180
|
+
generators.each do |generator|
|
|
181
|
+
sample = generator.uniform(lower, upper)
|
|
182
|
+
assert_operator sample, :>, lower
|
|
183
|
+
assert_operator sample, :<, upper
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
should "return the only representable interior float when bounds are very close" do
|
|
189
|
+
lower = 1.0
|
|
190
|
+
interior = lower.next_float
|
|
191
|
+
upper = interior.next_float
|
|
192
|
+
|
|
193
|
+
assert_equal interior, SimpleRandom.new(1179647999, 2422800383).uniform(lower, upper)
|
|
194
|
+
assert_equal interior, SimpleRandom.new(4294967296, 4294967296).uniform(lower, upper)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
should "keep samples inside integer bounds that round during float conversion" do
|
|
198
|
+
lower = 2 ** 53 + 3
|
|
199
|
+
upper = 2 ** 53 + 5
|
|
200
|
+
sample = SimpleRandom.new(1179647999, 2422800383).uniform(lower, upper)
|
|
201
|
+
|
|
202
|
+
assert_operator sample, :>, lower
|
|
203
|
+
assert_operator sample, :<, upper
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
should "reject bounds with no representable interior float" do
|
|
207
|
+
[[1.0, 1.0.next_float], [-1.0, (-1.0).next_float], [0.0, 0.0.next_float]].each do |lower, upper|
|
|
208
|
+
assert_raises(ArgumentError) { @r.uniform(lower, upper) }
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
107
212
|
should "generate uniformly random numbers with mean approximately 0.5" do
|
|
108
213
|
numbers = generate_numbers(@r, :uniform)
|
|
109
214
|
epsilon = (0.5 - numbers.mean).abs
|
|
@@ -180,6 +285,40 @@ class TestSimpleRandom < MiniTest::Test
|
|
|
180
285
|
assert @r.gamma(2.3, 2)
|
|
181
286
|
end
|
|
182
287
|
|
|
288
|
+
should "reject gamma shapes that cannot be represented as finite floats" do
|
|
289
|
+
[Float::INFINITY, -Float::INFINITY, Float::NAN, 10 ** 400].each do |shape|
|
|
290
|
+
Timeout.timeout(2) do
|
|
291
|
+
assert_raises(ArgumentError) { @r.gamma(shape, 1) }
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
should "reject infinite shapes in distributions that use gamma" do
|
|
297
|
+
[
|
|
298
|
+
[:chi_square, Float::INFINITY],
|
|
299
|
+
[:inverse_gamma, Float::INFINITY, 1],
|
|
300
|
+
[:beta, 1, Float::INFINITY],
|
|
301
|
+
[:student_t, Float::INFINITY],
|
|
302
|
+
[:dirichlet, 1, Float::INFINITY]
|
|
303
|
+
].each do |method, *args|
|
|
304
|
+
Timeout.timeout(2) do
|
|
305
|
+
assert_raises(ArgumentError) { @r.public_send(method, *args) }
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
should "finish gamma sampling with fixed-point seeds" do
|
|
311
|
+
r = SimpleRandom.new(1179647999, 2422800383)
|
|
312
|
+
|
|
313
|
+
Timeout.timeout(2) do
|
|
314
|
+
[0.5, 1, 5].each do |shape|
|
|
315
|
+
sample = r.gamma(shape, 1)
|
|
316
|
+
assert sample.finite?
|
|
317
|
+
assert_operator sample, :>, 0
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
|
|
183
322
|
should "generate a random number sampled from an inverse gamma distribution" do
|
|
184
323
|
assert @r.inverse_gamma(5, 2.3)
|
|
185
324
|
assert @r.inverse_gamma(5.7, 2.8)
|
|
@@ -217,6 +356,31 @@ class TestSimpleRandom < MiniTest::Test
|
|
|
217
356
|
@r = MultiThreadedSimpleRandom.instance
|
|
218
357
|
end
|
|
219
358
|
|
|
359
|
+
should "reuse the same generator across calls and fibers in one thread" do
|
|
360
|
+
assert_same @r, MultiThreadedSimpleRandom.instance
|
|
361
|
+
assert_same @r, Fiber.new { MultiThreadedSimpleRandom.instance }.resume
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
should "keep generators separate between threads and subclasses" do
|
|
365
|
+
other = Thread.new { MultiThreadedSimpleRandom.instance }.value
|
|
366
|
+
subclass = Class.new(MultiThreadedSimpleRandom)
|
|
367
|
+
|
|
368
|
+
refute_same @r, other
|
|
369
|
+
assert_instance_of subclass, subclass.instance
|
|
370
|
+
assert_same subclass.instance, subclass.instance
|
|
371
|
+
refute_same @r, subclass.instance
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
should "allow generators from finished threads to be garbage collected" do
|
|
375
|
+
references = Array.new(20) do
|
|
376
|
+
Thread.new { WeakRef.new(MultiThreadedSimpleRandom.instance) }.value
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
3.times { GC.start }
|
|
380
|
+
|
|
381
|
+
assert references.none?(&:weakref_alive?)
|
|
382
|
+
end
|
|
383
|
+
|
|
220
384
|
should "work independently in every thread" do
|
|
221
385
|
sample_count = 10
|
|
222
386
|
thread_count = 10
|
metadata
CHANGED
|
@@ -1,72 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simple-random
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- John D. Cook
|
|
8
8
|
- Jason Adams
|
|
9
|
-
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date: 2026-
|
|
13
|
-
dependencies:
|
|
14
|
-
- !ruby/object:Gem::Dependency
|
|
15
|
-
name: minitest
|
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
|
17
|
-
requirements:
|
|
18
|
-
- - ">="
|
|
19
|
-
- !ruby/object:Gem::Version
|
|
20
|
-
version: '0'
|
|
21
|
-
type: :development
|
|
22
|
-
prerelease: false
|
|
23
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
-
requirements:
|
|
25
|
-
- - ">="
|
|
26
|
-
- !ruby/object:Gem::Version
|
|
27
|
-
version: '0'
|
|
28
|
-
- !ruby/object:Gem::Dependency
|
|
29
|
-
name: shoulda
|
|
30
|
-
requirement: !ruby/object:Gem::Requirement
|
|
31
|
-
requirements:
|
|
32
|
-
- - ">="
|
|
33
|
-
- !ruby/object:Gem::Version
|
|
34
|
-
version: '0'
|
|
35
|
-
type: :development
|
|
36
|
-
prerelease: false
|
|
37
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
38
|
-
requirements:
|
|
39
|
-
- - ">="
|
|
40
|
-
- !ruby/object:Gem::Version
|
|
41
|
-
version: '0'
|
|
42
|
-
- !ruby/object:Gem::Dependency
|
|
43
|
-
name: jeweler
|
|
44
|
-
requirement: !ruby/object:Gem::Requirement
|
|
45
|
-
requirements:
|
|
46
|
-
- - "~>"
|
|
47
|
-
- !ruby/object:Gem::Version
|
|
48
|
-
version: 2.0.1
|
|
49
|
-
type: :development
|
|
50
|
-
prerelease: false
|
|
51
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
-
requirements:
|
|
53
|
-
- - "~>"
|
|
54
|
-
- !ruby/object:Gem::Version
|
|
55
|
-
version: 2.0.1
|
|
56
|
-
- !ruby/object:Gem::Dependency
|
|
57
|
-
name: simplecov
|
|
58
|
-
requirement: !ruby/object:Gem::Requirement
|
|
59
|
-
requirements:
|
|
60
|
-
- - ">="
|
|
61
|
-
- !ruby/object:Gem::Version
|
|
62
|
-
version: '0'
|
|
63
|
-
type: :development
|
|
64
|
-
prerelease: false
|
|
65
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
66
|
-
requirements:
|
|
67
|
-
- - ">="
|
|
68
|
-
- !ruby/object:Gem::Version
|
|
69
|
-
version: '0'
|
|
11
|
+
date: 2026-11-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
70
13
|
description: Simple Random Number Generator including Beta, Cauchy, Chi square, Exponential,
|
|
71
14
|
Gamma, Inverse Gamma, Laplace (double exponential), Normal, Student t, Uniform,
|
|
72
15
|
and Weibull. Ported from John D. Cook's C# Code.
|
|
@@ -78,7 +21,6 @@ extra_rdoc_files:
|
|
|
78
21
|
- README.md
|
|
79
22
|
files:
|
|
80
23
|
- ".document"
|
|
81
|
-
- ".travis.yml"
|
|
82
24
|
- Gemfile
|
|
83
25
|
- LICENSE
|
|
84
26
|
- README.md
|
|
@@ -94,7 +36,6 @@ homepage: http://github.com/ealdent/simple-random
|
|
|
94
36
|
licenses:
|
|
95
37
|
- CDDL-1.0
|
|
96
38
|
metadata: {}
|
|
97
|
-
post_install_message:
|
|
98
39
|
rdoc_options: []
|
|
99
40
|
require_paths:
|
|
100
41
|
- lib
|
|
@@ -109,8 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
109
50
|
- !ruby/object:Gem::Version
|
|
110
51
|
version: '0'
|
|
111
52
|
requirements: []
|
|
112
|
-
rubygems_version: 3.
|
|
113
|
-
signing_key:
|
|
53
|
+
rubygems_version: 3.6.9
|
|
114
54
|
specification_version: 4
|
|
115
55
|
summary: Simple Random Number Generator
|
|
116
56
|
test_files: []
|
data/.travis.yml
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# single test suite, non-parallel build.
|
|
2
|
-
|
|
3
|
-
env:
|
|
4
|
-
global:
|
|
5
|
-
- CC_TEST_REPORTER_ID=fc1330064eb82caf0f39ecd509e3e1c5b4fc5ca8b02fb9fa63d6e8a92da5ad18
|
|
6
|
-
language: ruby
|
|
7
|
-
rvm:
|
|
8
|
-
- 2.6
|
|
9
|
-
- 2.5
|
|
10
|
-
- 2.4
|
|
11
|
-
- 2.3.6
|
|
12
|
-
- 2.2.0
|
|
13
|
-
before_install:
|
|
14
|
-
- gem install bundler --version 1.17.3
|
|
15
|
-
before_script:
|
|
16
|
-
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
|
17
|
-
- chmod +x ./cc-test-reporter
|
|
18
|
-
- ./cc-test-reporter before-build
|
|
19
|
-
script:
|
|
20
|
-
- bundle exec rake test
|
|
21
|
-
after_script:
|
|
22
|
-
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|