random_bell 0.1.1 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8e2d2c6b448d735826d4bbfa7dab38c3ef046e7
4
- data.tar.gz: 2ccfb6c3f429860a0501785fc0585c4d97b45b2d
3
+ metadata.gz: a2f841d0dc7315bbccd0e09bb867f6fa3223fc9c
4
+ data.tar.gz: 318eb5a9bb67687eba4bd8a5151c54d39fcbe47b
5
5
  SHA512:
6
- metadata.gz: 39671a2d895f381db1e41e2a5137e01ce12065dd40780f21814851a1eee317641bec60b0518365524a5492334730af2a9bc242b886e40ebc1483f5b06cdbb068
7
- data.tar.gz: ba678c93052cae0515dbe6e3a0a20f8d8dc926db7ba0d54c7d7fa3ebe6c4832abf9fce29e24ace585499d4f9b5ebd6d58850c0448fd1386ddd1ceeb0a8287051
6
+ metadata.gz: 913930f257af0d0d3e62aac74fc83093597dda29a3b4a148e031a39f48cda817bf3bb0969c59c7b8c8c087cbbc51cc5a31f17b45c72e12c458a62cc9870798a7
7
+ data.tar.gz: e65b45d07605b9626c44216e0b54a128457c59ada044095eff5229d47255b1fa45837a26274258b7c45fc80de90313aea635d088ab50ccb76415f6d7bde88bfa
data/.travis.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
- - 2.1.0
3
+ - 2.2.0
4
+ - 2.3.0
5
+ - 2.4.0
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # RandomBell
2
2
 
3
- RandomBell is nomal(Gaussian) random number generator.
3
+ [![Build Status](https://travis-ci.org/s-osa/random_bell.svg?branch=0.1.1)](https://travis-ci.org/s-osa/random_bell)
4
+
5
+ RandomBell is Ruby gem to generate normal(Gaussian) random number.
4
6
 
5
7
  ## Installation
6
8
 
@@ -24,7 +26,185 @@ $ gem install random_bell
24
26
 
25
27
  ## Usage
26
28
 
27
- TODO: Write usage instructions here
29
+ ### Basic
30
+
31
+ #### Code
32
+
33
+ ```ruby
34
+ bell = RandomBell.new
35
+ bell.rand #=> 0.596308234257563
36
+ bell.rand #=> 0.4986734346841275
37
+ bell.rand #=> 0.6441978387725272
38
+ ```
39
+
40
+ #### Histogram
41
+
42
+ ```
43
+ +0.050: ***
44
+ +0.100: *****
45
+ +0.150: *********
46
+ +0.200: *************
47
+ +0.250: **********************
48
+ +0.300: **************************
49
+ +0.350: **********************************
50
+ +0.400: ****************************************
51
+ +0.450: ***********************************************
52
+ +0.500: *************************************************
53
+ +0.550: *************************************************
54
+ +0.600: ***********************************************
55
+ +0.650: **************************************
56
+ +0.700: **********************************
57
+ +0.750: **************************
58
+ +0.800: *********************
59
+ +0.850: *************
60
+ +0.900: ********
61
+ +0.950: *****
62
+ +1.000: ***
63
+ ```
64
+
65
+ ### Specify mean
66
+
67
+ #### Code
68
+
69
+ ```ruby
70
+ bell = RandomBell.new(mu: 0.75)
71
+ bell.rand
72
+ ```
73
+
74
+ #### Histogram
75
+
76
+ ```
77
+ +0.050:
78
+ +0.100:
79
+ +0.150:
80
+ +0.200:
81
+ +0.250: *
82
+ +0.300: **
83
+ +0.350: *****
84
+ +0.400: *********
85
+ +0.450: *************
86
+ +0.500: ********************
87
+ +0.550: ***************************
88
+ +0.600: ***********************************
89
+ +0.650: ****************************************
90
+ +0.700: ************************************************
91
+ +0.750: *************************************************
92
+ +0.800: *************************************************
93
+ +0.850: ********************************************
94
+ +0.900: *******************************************
95
+ +0.950: ***********************************
96
+ +1.000: *************************
97
+ ```
98
+
99
+ ### Specify standard diviation
100
+
101
+ #### Code
102
+
103
+ ```ruby
104
+ bell = RandomBell.new(sigma: 0.5)
105
+ bell.rand
106
+ ```
107
+
108
+ #### Histogram
109
+
110
+ ```
111
+ +0.050: *******************************
112
+ +0.100: *********************************
113
+ +0.150: *********************************
114
+ +0.200: *************************************
115
+ +0.250: ******************************************
116
+ +0.300: ********************************************
117
+ +0.350: ***********************************************
118
+ +0.400: *********************************************
119
+ +0.450: *************************************************
120
+ +0.500: ***********************************************
121
+ +0.550: ************************************************
122
+ +0.600: ***********************************************
123
+ +0.650: ******************************************
124
+ +0.700: ********************************************
125
+ +0.750: *********************************************
126
+ +0.800: ******************************************
127
+ +0.850: ****************************************
128
+ +0.900: *****************************************
129
+ +0.950: *********************************
130
+ +1.000: **********************************
131
+ ```
132
+
133
+ ### Specify range of number
134
+
135
+ #### Code
136
+
137
+ ```ruby
138
+ bell = RandomBell.new(range: 0.4..1.2)
139
+ bell.rand
140
+ ```
141
+
142
+ #### Histogram
143
+
144
+ ```
145
+ +0.440: *********************************************
146
+ +0.480: *************************************************
147
+ +0.520: **************************************************
148
+ +0.560: *************************************************
149
+ +0.600: ********************************************
150
+ +0.640: *****************************************
151
+ +0.680: ***********************************
152
+ +0.720: *****************************
153
+ +0.760: ************************
154
+ +0.800: ********************
155
+ +0.840: *************
156
+ +0.880: **********
157
+ +0.920: *******
158
+ +0.960: ****
159
+ +1.000: ***
160
+ +1.040: *
161
+ +1.080: *
162
+ +1.120:
163
+ +1.160:
164
+ +1.200:
165
+ ```
166
+
167
+ ### Composition
168
+
169
+ #### Code
170
+
171
+ ```ruby
172
+ bell = RandomBell.new(mu: 25, sigma: 2, range: 20..30)
173
+ bell.rand
174
+ ```
175
+
176
+ ```
177
+ +20.500: **
178
+ +21.000: *****
179
+ +21.500: *********
180
+ +22.000: *************
181
+ +22.500: ********************
182
+ +23.000: *************************
183
+ +23.500: *********************************
184
+ +24.000: *************************************
185
+ +24.500: ********************************************
186
+ +25.000: ************************************************
187
+ +25.500: **********************************************
188
+ +26.000: *******************************************
189
+ +26.500: ****************************************
190
+ +27.000: ********************************
191
+ +27.500: *************************
192
+ +28.000: *******************
193
+ +28.500: ************
194
+ +29.000: *******
195
+ +29.500: *****
196
+ +30.000: **
197
+ ```
198
+
199
+ ### Tips
200
+
201
+ `RandomBell#to_histogram` returns primitive histogram string.
202
+ The histograms in this page are drawn by this method.
203
+
204
+ ```ruby
205
+ bell = RandomBell.new
206
+ puts bell.to_histogram
207
+ ```
28
208
 
29
209
  ## Contributing
30
210
 
data/lib/random_bell.rb CHANGED
@@ -1,21 +1,30 @@
1
1
  require "random_bell/version"
2
2
 
3
3
  class RandomBell
4
- def initialize(mu: 0.5, sigma: 0.2, range: 0.0..1.0, method: :box_muller)
4
+ # @param mu [Float]
5
+ # @param sigma [Float]
6
+ # @param range [Range]
7
+ # @param seed [Integer]
8
+ # @param method [Symbol] Method to generate standard normal distribution (:box_muller or :central_limit)
9
+ def initialize(mu: 0.5, sigma: 0.2, range: 0.0..1.0, seed: Random.new_seed, method: :box_muller)
5
10
  @mu = mu
6
11
  @sigma = sigma
7
12
  @range = range
13
+ @rand_generator = Random.new(seed)
8
14
  @method = method
9
15
  end
10
16
 
17
+ # @return [Float]
11
18
  def standard
12
19
  send(@method)
13
20
  end
14
21
 
22
+ # @return [Float]
15
23
  def normal
16
24
  @mu + standard * @sigma
17
25
  end
18
26
 
27
+ # @return [Float]
19
28
  def rand
20
29
  loop do
21
30
  num = normal
@@ -23,6 +32,7 @@ class RandomBell
23
32
  end
24
33
  end
25
34
 
35
+ # @return [String]
26
36
  def to_histogram(rows: 20, size: 10 ** 4)
27
37
  histogram = ""
28
38
 
@@ -30,7 +40,7 @@ class RandomBell
30
40
 
31
41
  klasses, klass_width = [], (@range.max - @range.min).to_f / rows
32
42
  (0..rows).each_cons(2) do |left, right|
33
- klasses << Range.new(@range.min + klass_width * left, @range.min + klass_width *right)
43
+ klasses << Range.new(@range.min + klass_width * left, @range.min + klass_width * right)
34
44
  end
35
45
 
36
46
  divider = (klasses.map{|klass| numbers.select{|num| klass.include?(num) }.size }.max / 50.0).ceil
@@ -44,16 +54,29 @@ class RandomBell
44
54
 
45
55
  private
46
56
 
57
+ # @return [Float]
47
58
  def box_muller
48
- theta = 2 * Math::PI * Random.rand
49
- r = Math.sqrt( -2 * Math.log(Random.rand) )
50
- z1, z2 = r * Math.cos(theta), r * Math.sin(theta)
51
- [z1, z2].sample
59
+ @box_muller_queue ||= []
60
+
61
+ if @box_muller_queue.empty?
62
+ theta = 2 * Math::PI * random_number
63
+ r = Math.sqrt( -2 * Math.log(random_number) )
64
+ z1, z2 = r * Math.cos(theta), r * Math.sin(theta)
65
+ @box_muller_queue += [z1, z2]
66
+ end
67
+
68
+ @box_muller_queue.shift
52
69
  end
53
70
 
71
+ # @return [Float]
54
72
  def central_limit
55
73
  arr = []
56
- 12.times{ arr << Random.rand }
74
+ 12.times{ arr << random_number }
57
75
  arr.reduce(0.0, &:+) - 6
58
76
  end
77
+
78
+ # @return [Float]
79
+ def random_number
80
+ @rand_generator.rand
81
+ end
59
82
  end
@@ -1,3 +1,3 @@
1
1
  class RandomBell
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/random_bell.gemspec CHANGED
@@ -20,5 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec", "~> 2"
23
+ spec.add_development_dependency "rspec", "~> 3"
24
24
  end
@@ -118,6 +118,19 @@ describe RandomBell do
118
118
  end
119
119
  end
120
120
  end
121
+
122
+ context "seed given" do
123
+ before do
124
+ @bell1 = RandomBell.new(seed: 1234)
125
+ @bell2 = RandomBell.new(seed: 1234)
126
+ end
127
+
128
+ it "should return same sequence" do
129
+ 100.times do
130
+ expect(@bell1.rand).to eq @bell2.rand
131
+ end
132
+ end
133
+ end
121
134
  end
122
135
 
123
136
  describe "#to_histogram" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random_bell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OSA Shunsuke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-06 00:00:00.000000000 Z
11
+ date: 2017-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '2'
47
+ version: '3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '2'
54
+ version: '3'
55
55
  description: Normal(Gaussian) random number generator.
56
56
  email:
57
57
  - hhelibebcnofnenamg@gmail.com
@@ -64,7 +64,6 @@ files:
64
64
  - ".travis.yml"
65
65
  - Gemfile
66
66
  - LICENSE
67
- - LICENSE.txt
68
67
  - README.md
69
68
  - Rakefile
70
69
  - lib/random_bell.rb
@@ -92,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
91
  version: '0'
93
92
  requirements: []
94
93
  rubyforge_project:
95
- rubygems_version: 2.2.2
94
+ rubygems_version: 2.6.13
96
95
  signing_key:
97
96
  specification_version: 4
98
97
  summary: Normal(Gaussian) random number generator.
data/LICENSE.txt DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2014 OSA Shunsuke
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.