guided_randomness 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README +15 -0
- data/Rakefile +9 -0
- data/guided_randomness.gemspec +30 -0
- data/lib/guided_randomness.rb +22 -0
- data/lib/guided_randomness/version.rb +3 -0
- data/test/test.rb +68 -0
- metadata +62 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
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).
|
2
|
+
|
3
|
+
Instalation:
|
4
|
+
|
5
|
+
gem install guided_randomness
|
6
|
+
|
7
|
+
|
8
|
+
Usage:
|
9
|
+
|
10
|
+
require 'guided_randomness'
|
11
|
+
[1,2,3].get_rand([0.5, 0.3, 0.2])
|
12
|
+
|
13
|
+
or
|
14
|
+
require 'guided_randomness'
|
15
|
+
[1,2,3].get_rand([4,12,9])
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "guided_randomness/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "guided_randomness"
|
7
|
+
s.version = GuidedRandomness::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Piotr Esse Szmielew"]
|
10
|
+
s.email = ["p.szmielew@roflcopter.pl"]
|
11
|
+
s.homepage = ""
|
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
|
+
s.description = %q{
|
14
|
+
Usage:
|
15
|
+
|
16
|
+
require 'guided_randomness'
|
17
|
+
[1,2,3].get_rand([0.5, 0.3, 0.2])
|
18
|
+
|
19
|
+
or
|
20
|
+
require 'guided_randomness'
|
21
|
+
[1,2,3].get_rand([4,12,9])
|
22
|
+
}
|
23
|
+
|
24
|
+
s.rubyforge_project = "guided_randomness"
|
25
|
+
|
26
|
+
s.files = `git ls-files`.split("\n")
|
27
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
28
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
end
|
@@ -0,0 +1,22 @@
|
|
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
|
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
|
+
end
|
21
|
+
|
22
|
+
end
|
data/test/test.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require './lib/guided_randomness'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class RandomnessTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_first_check_randomness_using_law_of_big_numbers
|
7
|
+
@array = ["a", "b", "c", "d", "e"]
|
8
|
+
@chances = [10, 2, 8, 5, 9]
|
9
|
+
helper
|
10
|
+
calculate_chances
|
11
|
+
check_asserts(0.05)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_second_check_randomness_using_law_of_big_numbers
|
15
|
+
@array = ["a", "b", "c", "d", "e", "g", "h", "i", "j", "k"]
|
16
|
+
@chances = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]
|
17
|
+
helper
|
18
|
+
calculate_chances
|
19
|
+
check_asserts(0.05)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_third_check_randomness_using_law_of_big_numbers
|
23
|
+
@array = (1..500).to_a
|
24
|
+
@chances = [0.1]*500
|
25
|
+
helper
|
26
|
+
calculate_chances
|
27
|
+
check_asserts(0.05)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_fourth_check_randomness_using_law_of_big_numbers
|
31
|
+
@array = (1..300).to_a
|
32
|
+
@chances = (1..300).to_a
|
33
|
+
helper
|
34
|
+
calculate_chances
|
35
|
+
check_asserts(0.05)
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def helper
|
43
|
+
@test_hash = {}
|
44
|
+
@array.each do |a|
|
45
|
+
@test_hash[a] = 0
|
46
|
+
end
|
47
|
+
@total = @chances.inject { |x, y| x + y }
|
48
|
+
end
|
49
|
+
|
50
|
+
def calculate_chances
|
51
|
+
10000.times do
|
52
|
+
@test_hash[@array.get_rand(@chances)] += 1
|
53
|
+
end
|
54
|
+
@test_hash.each_pair do |k,v|
|
55
|
+
@test_hash[k] = v / 10000.0
|
56
|
+
end
|
57
|
+
@chances = @chances.map do |x|
|
58
|
+
x / @total.to_f
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def check_asserts(deviation)
|
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}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: guided_randomness
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Piotr Esse Szmielew
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-12 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "\n Usage:\n\n require 'guided_randomness'\n [1,2,3].get_rand([0.5, 0.3, 0.2])\n\n or\n require 'guided_randomness'\n [1,2,3].get_rand([4,12,9])\n "
|
17
|
+
email:
|
18
|
+
- p.szmielew@roflcopter.pl
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- Gemfile
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- guided_randomness.gemspec
|
31
|
+
- lib/guided_randomness.rb
|
32
|
+
- lib/guided_randomness/version.rb
|
33
|
+
- test/test.rb
|
34
|
+
homepage: ""
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project: guided_randomness
|
57
|
+
rubygems_version: 1.8.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: 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).
|
61
|
+
test_files:
|
62
|
+
- test/test.rb
|