guided_randomness 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,22 +1,13 @@
1
1
  class Array
2
-
3
- def get_rand(arr)
4
- raise "Wrong number of arguments" unless arr.size == self.size
5
- # raise "Currently working only for arrays smaller then 200 elements" if arr.size > 199
6
- total = arr.inject { |x, y| x+y }
7
- hash = { }
8
- counter = (arr.size / 100)+ 1
9
- self.each_with_index do |arg, i|
10
- hash[arg] = ((arr[i].to_f / total.to_f) * counter * 100).round
2
+ def get_rand(weights)
3
+ raise "Wrong number of array elements!" unless weights.size == self.size
4
+
5
+ total = weights.inject(0, :+)
6
+ point = Kernel.rand * total
7
+
8
+ self.each_with_index do |element, i|
9
+ return element if weights[i] >= point
10
+ point -= weights[i]
11
11
  end
12
- array_to_load = []
13
- hash.each_pair do |k,v|
14
- v.times do
15
- array_to_load << k
16
- end
17
- end
18
- value = rand(3) - 1
19
- array_to_load.sort { |x,y| rand(3)-1 }.first
20
12
  end
21
-
22
- end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module GuidedRandomness
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: guided_randomness
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Piotr Esse Szmielew