box_muller 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ef931bf8a7e2c087901fbcb6ac5de61e8d61c4cdf41bca57263edd087d8ebc1d
4
+ data.tar.gz: edd2203497f6c110ec77e789a9ffba1ba7b1937b471403efdc0fddf9b91537a6
5
+ SHA512:
6
+ metadata.gz: 420ec0e65bc6a2d9be0cbf1a994c0b35717a168ef524b75fb27777329bffeb3c9ce6a41c6e287861db339eee5740cbdecdfa15d0fdf9b49ba60a758cdbde63b8
7
+ data.tar.gz: 49c90aaf1056c5ce45b81d878842222f2e27d6ea23664aac1b120ff254debd128e61c81fe3ea7233e4a82f916adc97256d850f16f0f5ae2c7539ceb28bb5ae70
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in box_muller.gemspec
6
+ gemspec
7
+
8
+ group :development, :test do
9
+ gem "pry"
10
+ gem "pry-byebug"
11
+ end
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ box_muller (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (10.0.0)
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ method_source (0.9.0)
13
+ pry (0.11.3)
14
+ coderay (~> 1.1.0)
15
+ method_source (~> 0.9.0)
16
+ pry-byebug (3.6.0)
17
+ byebug (~> 10.0)
18
+ pry (~> 0.10)
19
+ rake (10.5.0)
20
+ rspec (3.7.0)
21
+ rspec-core (~> 3.7.0)
22
+ rspec-expectations (~> 3.7.0)
23
+ rspec-mocks (~> 3.7.0)
24
+ rspec-core (3.7.1)
25
+ rspec-support (~> 3.7.0)
26
+ rspec-expectations (3.7.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.7.0)
29
+ rspec-mocks (3.7.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.7.0)
32
+ rspec-support (3.7.1)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ box_muller!
39
+ bundler (~> 1.16)
40
+ pry
41
+ pry-byebug
42
+ rake (~> 10.0)
43
+ rspec (~> 3.0)
44
+
45
+ BUNDLED WITH
46
+ 1.16.1
@@ -0,0 +1,60 @@
1
+ # BoxMuller
2
+
3
+ BoxMuller implementation in Ruby.
4
+
5
+ <!-- TOC -->
6
+
7
+ - [BoxMuller](#boxmuller)
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [`BoxMuller::Distributor`](#boxmullerdistributor)
11
+ - [`#distribute`](#distribute)
12
+ - [Development](#development)
13
+ - [Contributing](#contributing)
14
+
15
+ <!-- /TOC -->
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'box_muller'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install box_muller
32
+
33
+ ## Usage
34
+
35
+ ### `BoxMuller::Distributor`
36
+
37
+ #### `#distribute`
38
+
39
+ ```ruby
40
+ distributor = BoxMuller::Distributor.new
41
+
42
+ p distributor.distribute(total: 1000, count: 10)
43
+ => [102, 94, 105, 110, 115, 103, 83, 101, 104, 83]
44
+
45
+ p distributor.distribute(total: 1000, count: 10, variance: 1.0)
46
+ => [99, 100, 100, 101, 99, 100, 100, 102, 100, 99]
47
+
48
+ p distributor.distribute(total: 1000, count: 10, variance: 100.0)
49
+ => [29, -76, 101, 329, -20, 60, 198, 284, -162, 257]
50
+ ```
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kenju/box_muller.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "box_muller"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "box_muller/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "box_muller"
8
+ spec.version = BoxMuller::VERSION
9
+ spec.authors = ["kenju"]
10
+ spec.email = ["ken901waga@gmail.com"]
11
+
12
+ spec.summary = %q{BoxMuller implementation in Ruby}
13
+ spec.homepage = "https://github.com/kenju/box_muller_rb"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,5 @@
1
+ require "box_muller/version"
2
+ require "box_muller/distributor"
3
+
4
+ module BoxMuller
5
+ end
@@ -0,0 +1,37 @@
1
+ module BoxMuller
2
+ class Distributor
3
+ def initialize
4
+ end
5
+
6
+ def distribute(total:, count:, variance: 10.0)
7
+ tmp_total = total
8
+ count.times.map { |i|
9
+ if i == count - 1 # reaches the last item
10
+ tmp_total
11
+ else
12
+ current = gaussian(mean: tmp_total / (count - i), variance: variance).first.round
13
+ tmp_total = tmp_total - current
14
+ current
15
+ end
16
+ }
17
+ end
18
+
19
+ private
20
+
21
+ # credit to @antonakos
22
+ # at https://stackoverflow.com/questions/5825680/code-to-generate-gaussian-normally-distributed-random-numbers-in-ruby
23
+ def gaussian(mean:, variance:)
24
+ r1 = rand()
25
+ r2 = rand()
26
+
27
+ theta = 2 * Math::PI * r2
28
+ z1 = Math.sqrt(-2 * Math.log(1 - r1)) * Math.cos(theta)
29
+ z2 = Math.sqrt(-2 * Math.log(1 - r1)) * Math.sin(theta)
30
+
31
+ x = mean + variance * z1
32
+ y = mean + variance * z2
33
+
34
+ [x, y]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module BoxMuller
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: box_muller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kenju
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - ken901waga@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - box_muller.gemspec
72
+ - lib/box_muller.rb
73
+ - lib/box_muller/distributor.rb
74
+ - lib/box_muller/version.rb
75
+ homepage: https://github.com/kenju/box_muller_rb
76
+ licenses: []
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: BoxMuller implementation in Ruby
98
+ test_files: []