newcomb 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b718f0e37121533e31fe9c789e912422c12406f2
4
+ data.tar.gz: 49c5cb53702a3f6f8db7f2cb34c0fea50d732337
5
+ SHA512:
6
+ metadata.gz: 57922988b4fc725ff86fc6d591e6398c86fdfbf378ef7aa627031834d0b979c7b5ef7dbfb68303513849eb671f417867bda55d8b17e6ac056746426b3646c81f
7
+ data.tar.gz: e5b2a92ddc77101a22f7899c8f1a70433cd5b16d4e991c2940da091bd510f51f2643414823737775d6733cf5512bb72ff41d25339f62d919ecc66687714988ea
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format documentation
3
+ --order random
4
+ --require spec_helper
@@ -0,0 +1,19 @@
1
+ branches:
2
+ only:
3
+ - master
4
+ env:
5
+ global:
6
+ secure: | # CODECLIMATE_REPO_TOKEN
7
+ XnaAxp9pN9OR+jSyLUXQwKkMVcUfNy+7muWXKLQRi/zIh2ObGBCEXTww/8mP
8
+ hwv4Rf8a4n7+gpQhmv6MEx3xwdArkxLChs1EX79XGNN9/g/DBPtfoiKB3FLd
9
+ UUJTuPurAlOklt/s+XvJTV5h50ThpvutSqkSenO5me8v/ODO6hw=
10
+ language: ruby
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
14
+ rvm:
15
+ - 1.9.3
16
+ - "2.0"
17
+ - "2.1"
18
+ - ruby-head
19
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem "codeclimate-test-reporter", require: false
7
+ gem "rspec", "~> 3.0"
8
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Steve Richert
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.
@@ -0,0 +1,36 @@
1
+ # Newcomb
2
+
3
+ Generate random numbers that adhere to [Benford's Law](http://en.wikipedia.org/wiki/Benford's_law)
4
+
5
+ [![Gem Version](https://img.shields.io/gem/v/newcomb.svg?style=flat)](http://rubygems.org/gems/newcomb)
6
+ [![Build Status](https://img.shields.io/travis/laserlemon/newcomb/master.svg?style=flat)](https://travis-ci.org/laserlemon/newcomb)
7
+ [![Code Climate](https://img.shields.io/codeclimate/github/laserlemon/newcomb.svg?style=flat)](https://codeclimate.com/github/laserlemon/newcomb)
8
+ [![Code Coverage](http://img.shields.io/codeclimate/coverage/github/laserlemon/newcomb.svg?style=flat)](https://codeclimate.com/github/laserlemon/newcomb)
9
+ [![Dependency Status](https://img.shields.io/gemnasium/laserlemon/newcomb.svg?style=flat)](https://gemnasium.com/laserlemon/newcomb)
10
+
11
+ ## Usage
12
+
13
+ Newcomb works very much like `Kernel.rand` or `SecureRandom.random_number`. In
14
+ order to fetch a random float between 0 (inclusive) and 1 (exclusive):
15
+
16
+ ```ruby
17
+ Newcomb.random_number # => 0.49552091973604767
18
+ ```
19
+
20
+ To fetch a random positive integer, provide the upper (exclusive) limit:
21
+
22
+ ```ruby
23
+ Newcomb.random_number(100) # => 16
24
+ ```
25
+
26
+ Over a sufficiently large sample size, the distribution of Newcomb's random
27
+ numbers will demonstrate Benford's Law.
28
+
29
+ ## History
30
+
31
+ While Benford's Law is named after American scientist [Frank Benford](http://en.wikipedia.org/wiki/Frank_Benford),
32
+ the principle was original discovered by [Simon Newcomb](http://en.wikipedia.org/wiki/Simon_Newcomb)
33
+ over fifty years earlier. In addition to his numerous accomplishments in
34
+ mathematics and astronomy, Newcomb had a fantastic beard.
35
+
36
+ ![Exhibit A](http://upload.wikimedia.org/wikipedia/commons/thumb/9/92/Simon_Newcomb_01.jpg/640px-Simon_Newcomb_01.jpg)
@@ -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,11 @@
1
+ require "securerandom"
2
+
3
+ module Newcomb
4
+ def self.random_number(n = 0)
5
+ if n > 0
6
+ (random_number * n).floor
7
+ else
8
+ (10 ** SecureRandom.random_number - 1) / 9
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "newcomb"
5
+ spec.version = "1.0.0"
6
+
7
+ spec.author = "Steve Richert"
8
+ spec.email = "steve.richert@gmail.com"
9
+ spec.summary = "Generate natural random numbers"
10
+ spec.description = "Generate random numbers that adhere to Benford's Law"
11
+ spec.homepage = "https://github.com/laserlemon/newcomb"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.test_files = spec.files.grep(/^spec/)
16
+
17
+ spec.add_development_dependency "bundler", "~> 1.6"
18
+ spec.add_development_dependency "rake", "~> 10.3"
19
+ end
@@ -0,0 +1,68 @@
1
+ describe Newcomb do
2
+ describe ".random_number" do
3
+ let(:sample_size) { 1_000_000 }
4
+
5
+ def assert_benford(&block)
6
+ distribution = build_distribution(&block)
7
+
8
+ expect(distribution.keys).to match_array([1, 2, 3, 4, 5, 6, 7, 8, 9])
9
+
10
+ distribution.sort.each do |i, count|
11
+ expect(distribution[i]).to be_within(tolerance(i)).of(expected_count(i))
12
+ end
13
+ end
14
+
15
+ def build_distribution(&block)
16
+ sample_size.times.with_object({}) do |_, memo|
17
+ digit = block.call
18
+ memo[digit] ||= 0
19
+ memo[digit] += 1
20
+ end
21
+ end
22
+
23
+ def build_sample(&block)
24
+ sample_size.times.map(&block)
25
+ end
26
+
27
+ # TODO: Update to assert adherence to within one standard deviation
28
+ def tolerance(digit)
29
+ (expected_count(digit) * 0.01).round
30
+ end
31
+
32
+ def expected_count(digit)
33
+ (((Math.log(digit + 1) - Math.log(digit)) / Math.log(10)) * sample_size).round
34
+ end
35
+
36
+ context "when given no arguments" do
37
+ it "falls between zero (inclusive) and one (exclusive)" do
38
+ sample = build_sample { Newcomb.random_number }
39
+
40
+ expect(sample.map(&:class).uniq).to eq([Float])
41
+ expect(sample.map(&:floor).uniq).to eq([0])
42
+ end
43
+
44
+ it "is distributed according to Benford's law" do
45
+ assert_benford do
46
+ (Newcomb.random_number * 9).floor + 1
47
+ end
48
+ end
49
+ end
50
+
51
+ context "when given an argument" do
52
+ it "falls between zero (inclusive) and the given number (exclusive)" do
53
+ sample = sample_size.times.map { Newcomb.random_number(13) }
54
+
55
+ expect(sample.map(&:class).uniq).to eq([Fixnum])
56
+ floors = sample.map(&:floor)
57
+ expect(floors.min).to be >= 0
58
+ expect(floors.max).to be < 13
59
+ end
60
+
61
+ it "is distributed according to Benford's law" do
62
+ assert_benford do
63
+ Newcomb.random_number(9) + 1
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,6 @@
1
+ if ENV["CODECLIMATE_REPO_TOKEN"]
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
4
+ end
5
+
6
+ require "newcomb"
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newcomb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Richert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-15 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ description: Generate random numbers that adhere to Benford's Law
42
+ email: steve.richert@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - ".rspec"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/newcomb.rb
55
+ - newcomb.gemspec
56
+ - spec/newcomb_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: https://github.com/laserlemon/newcomb
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Generate natural random numbers
82
+ test_files:
83
+ - spec/newcomb_spec.rb
84
+ - spec/spec_helper.rb