guided_randomness 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
@@ -6,12 +6,15 @@ Gem::Specification.new do |s|
6
6
  s.name = "guided_randomness"
7
7
  s.version = GuidedRandomness::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Piotr Esse Szmielew"]
10
- s.email = ["p.szmielew@roflcopter.pl"]
9
+ s.authors = ["Piotr Esse Szmielew", "Radosław Bułat", "Robert Pankowecki"]
10
+ s.email = ["p.szmielew@roflcopter.pl", "radek.bulat@gmail.com", "robert.pankowecki@gmail.com"]
11
11
  s.homepage = ""
12
12
  s.summary = %q{Simple gem for Ruby which extends Array class, and gave possibility to pick element from it at random, however with certain probability (probability is given for each element).}
13
13
  s.description = %q{Simple gem for Ruby which extends Array class, and gave possibility to pick element from it at random, however with certain probability (probability is given for each element). Adds the get_rand function to array class, which take array of chances as arguments (for example [1,2,3].get_rand([3,4,5]))}
14
14
  s.rubyforge_project = "guided_randomness"
15
+
16
+ s.add_dependency "bsearch", "~> 1.5"
17
+
15
18
  s.files = `git ls-files`.split("\n")
16
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
20
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -1,13 +1,29 @@
1
+ require 'rubygems'
2
+ require 'bsearch'
3
+
1
4
  class Array
5
+
2
6
  def get_rand(weights)
3
- raise "Wrong number of array elements!" unless weights.size == self.size
7
+ GuidedRandomness.new(self, weights).sample.first
8
+ end
9
+
10
+ end
4
11
 
5
- total = weights.inject(0, :+)
6
- point = Kernel.rand * total
12
+ class GuidedRandomness < Struct.new(:source, :weights)
7
13
 
8
- self.each_with_index do |element, i|
9
- return element if weights[i] >= point
10
- point -= weights[i]
14
+ def initialize(*params)
15
+ super
16
+ raise ArgumentError, "Wrong number of array elements!" if weights.size != source.size || weights.size == 0
17
+ @sums = weights.inject([]){|ary, weight| ary << ((ary.last || 0) + weight)}
18
+ @total = @sums.last
19
+ end
20
+
21
+ def sample(size = 1)
22
+ size.times.map do
23
+ rand = Kernel.rand*@total
24
+ index = @sums.bsearch_lower_boundary{|x| x <=> rand}
25
+ source[index]
11
26
  end
12
27
  end
13
- end
28
+
29
+ end
@@ -1,3 +1,3 @@
1
1
  module GuidedRandomness
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -61,7 +61,7 @@ class RandomnessTest < Test::Unit::TestCase
61
61
 
62
62
  def check_asserts(deviation)
63
63
  @array.each_with_index do |a, i|
64
- assert (@test_hash[a] - @chances[i]).abs < deviation, "Devation should be lower then #{deviation}, and actually it is: #{(@test_hash[a] - @chances[i]).abs}"
64
+ assert( (@test_hash[a] - @chances[i]).abs < deviation, "Devation should be lower then #{deviation}, and actually it is: #{(@test_hash[a] - @chances[i]).abs}" )
65
65
  end
66
66
  end
67
67
 
metadata CHANGED
@@ -2,20 +2,34 @@
2
2
  name: guided_randomness
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Piotr Esse Szmielew
9
+ - "Rados\xC5\x82aw Bu\xC5\x82at"
10
+ - Robert Pankowecki
9
11
  autorequire:
10
12
  bindir: bin
11
13
  cert_chain: []
12
14
 
13
- date: 2011-05-12 00:00:00 Z
14
- dependencies: []
15
-
15
+ date: 2011-05-13 00:00:00 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: bsearch
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ version: "1.5"
26
+ type: :runtime
27
+ version_requirements: *id001
16
28
  description: Simple gem for Ruby which extends Array class, and gave possibility to pick element from it at random, however with certain probability (probability is given for each element). Adds the get_rand function to array class, which take array of chances as arguments (for example [1,2,3].get_rand([3,4,5]))
17
29
  email:
18
30
  - p.szmielew@roflcopter.pl
31
+ - radek.bulat@gmail.com
32
+ - robert.pankowecki@gmail.com
19
33
  executables: []
20
34
 
21
35
  extensions: []
@@ -25,6 +39,7 @@ extra_rdoc_files: []
25
39
  files:
26
40
  - .gitignore
27
41
  - Gemfile
42
+ - LICENSE
28
43
  - README
29
44
  - Rakefile
30
45
  - guided_randomness.gemspec