randomer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in randomer.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Zhuang Sirui
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.
data/README.markdown ADDED
@@ -0,0 +1,82 @@
1
+ # Randomer
2
+
3
+ A gem for random everything.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'randomer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install randomer
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ # Get one in an array or a range random
23
+ range_number = 1..1000
24
+ array_string = ['cp', 'mv', 'ab', 'git', 'ruby']
25
+ puts Randomer::Ranger.rand_in range_number
26
+ # => 855
27
+ puts Randomer::Ranger.rand_in array_string
28
+ # => "ruby"
29
+
30
+ puts Randomer::Ranger.randmon_string 10
31
+ # => "myjxquvpsr"
32
+
33
+ puts Randomer::Ranger.randmon_string 10 :lower
34
+ # => "rviekbiyuy"
35
+
36
+ Randomer::Ranger.randmon_string 10, :upper
37
+ # => "WMIKCIOGVJ"
38
+
39
+ puts Randomer::Ranger.randmon_string(
40
+ 10,
41
+ :upper,
42
+ :lower
43
+ )
44
+ # => "RANTIrawWk"
45
+
46
+ puts Randomer::Ranger.randmon_string(
47
+ 10,
48
+ :symbol
49
+ )
50
+ # => "**'&-!/)(%"
51
+
52
+ puts Randomer::Ranger.randmon_string(
53
+ 50,
54
+ :symbol,
55
+ :upper,
56
+ :lower,
57
+ :number
58
+ )
59
+ # => "o.42zCQ1ER'W1Q#A+YmgNbOexl((Xk9YSO+BP029Z6R4g+LQBo"
60
+
61
+ # random with percent hash
62
+ percent_list = {
63
+ :a => 400, # Most probability get this!
64
+ :b => 1000, # Most probability get this!
65
+ :c => 1, # Amoust nerver get this!
66
+ :e => 0, # Never get this!
67
+ }
68
+ puts Randomer::Percent.pick_one percent_list
69
+ # => b
70
+ ```
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
79
+
80
+ ## TODO
81
+
82
+ 1. More random way!
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module Randomer
2
+ VERSION = "0.0.1"
3
+ end
data/lib/randomer.rb ADDED
@@ -0,0 +1,96 @@
1
+ require "randomer/version"
2
+
3
+ module Randomer
4
+ class Ranger
5
+ class << self
6
+ ##
7
+ # 一个范围内随机获取一个值
8
+ #
9
+ # [params] * Mixed range Range or Array
10
+ #
11
+ def rand_in(list, *args)
12
+ list = list.to_a if list.is_a? Range
13
+ return nil unless list.is_a? Array
14
+
15
+ count = list.count
16
+ index = get_rand_index count
17
+ return list[index]
18
+ end
19
+
20
+ ##
21
+ # 获取一个随机字符串
22
+ #
23
+ # [params] * Integer length
24
+ #
25
+ def randmon_string(length = 1, *args)
26
+ return '' unless length.is_a? Integer
27
+
28
+ all_chars = {
29
+ :upper => 'A'..'Z',
30
+ :lower => 'a'..'z',
31
+ :symbol => '!'..'/',
32
+ :number => '0'..'9',
33
+ }
34
+ letter = []
35
+ args.each do |arg|
36
+ if all_chars.keys.include? arg
37
+ letter += all_chars[arg].to_a
38
+ all_chars.delete arg
39
+ end
40
+ end
41
+ letter = all_chars[:lower].to_a if letter.count <= 0
42
+
43
+ string = ''
44
+ count = letter.count
45
+ length.times do
46
+ index = get_rand_index count
47
+ string += letter[index]
48
+ end
49
+ string
50
+ end
51
+
52
+ private
53
+
54
+ def get_rand_index(count, *args)
55
+ return rand(count)
56
+ end
57
+ end
58
+ end
59
+
60
+ class Percent
61
+ class << self
62
+ ##
63
+ # 根据一个hash中value指定的权重随机获取一个Symbol
64
+ #
65
+ # [params] Hash hash
66
+ #
67
+ # [return] Symbol key
68
+ #
69
+ # [usage]
70
+ # Randomer::Percent.pick_one({
71
+ # :a => 60,
72
+ # :b => 400,
73
+ # })
74
+ #
75
+ def pick_one(hash)
76
+ sum_value = 0
77
+ interval = []
78
+ keys = []
79
+ hash.each do |key, value|
80
+ return false unless key.is_a? Symbol
81
+ return false unless value.is_a? Integer
82
+
83
+ interval << (sum_value...(sum_value += value))
84
+ keys << key
85
+ end
86
+ return false if sum_value <= 0
87
+
88
+ count = interval.count
89
+ rand_result = rand sum_value
90
+ interval.each_with_index do |range, index|
91
+ return keys[index] if range.include? rand_result
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
data/randomer.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'randomer/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "randomer"
8
+ gem.version = Randomer::VERSION
9
+ gem.authors = ["Zhuang Sirui"]
10
+ gem.email = ["siriusibunny@gmail.com"]
11
+ gem.description = %q{A gem for random everything}
12
+ gem.summary = %q{Write a gem summary}
13
+ gem.homepage = "http://www.skzsr.com/"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Randomer::Percent do
4
+ before(:each) do
5
+ @percent_list = {
6
+ :a => 900,
7
+ :b => 1000,
8
+ :c => 870,
9
+ :e => 0,
10
+ }
11
+ end
12
+
13
+ it "Pick one by percent list" do
14
+ 1000.times do
15
+ result = Randomer::Percent.pick_one @percent_list
16
+ @percent_list.include?(result).should == true
17
+ (result == :e).should == false
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Randomer::Ranger do
4
+ before(:each) do
5
+ @list = {
6
+ :range_number => 1..1000,
7
+ :range_char => 'a'..'z',
8
+ :array_number => [1, 2, 3, 4, 5, 6, 100, 200, 300, 444, 555],
9
+ :array_string => ['ab', 'ssh', 'cp', 'cd', 'mv', 'git', 'rails'],
10
+ }
11
+ end
12
+
13
+ it "Random in some range" do
14
+ @list.each do |key, value|
15
+ result = Randomer::Ranger.rand_in value
16
+ value.include?(result).should == true
17
+ end
18
+ end
19
+
20
+ it "Get randmon lower string" do
21
+ string_length = 10
22
+ Randomer::Ranger.randmon_string string_length
23
+ Randomer::Ranger.randmon_string(
24
+ string_length,
25
+ :lower
26
+ )
27
+ end
28
+
29
+ it "Get randmon upper string" do
30
+ string_length = 10
31
+ Randomer::Ranger.randmon_string(
32
+ string_length,
33
+ :upper
34
+ )
35
+ end
36
+
37
+ it "Get randmon upper and lower string" do
38
+ string_length = 10
39
+ Randomer::Ranger.randmon_string(
40
+ string_length,
41
+ :upper,
42
+ :lower
43
+ )
44
+ end
45
+
46
+ it "Get randmon symbol string" do
47
+ string_length = 10
48
+ Randomer::Ranger.randmon_string(
49
+ string_length,
50
+ :symbol
51
+ )
52
+ end
53
+
54
+ it "Get randmon all string" do
55
+ string_length = 50
56
+ Randomer::Ranger.randmon_string(
57
+ string_length,
58
+ :symbol,
59
+ :upper,
60
+ :lower,
61
+ :number
62
+ )
63
+ end
64
+ end
@@ -0,0 +1,6 @@
1
+ require 'randomer'
2
+
3
+ RSpec.configure do |config|
4
+ config.color_enabled = true
5
+ config.formatter = 'documentation'
6
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: randomer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zhuang Sirui
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem for random everything
15
+ email:
16
+ - siriusibunny@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.markdown
25
+ - Rakefile
26
+ - lib/randomer.rb
27
+ - lib/randomer/version.rb
28
+ - randomer.gemspec
29
+ - spec/randomer_percent_spec.rb
30
+ - spec/randomer_ranger_spec.rb
31
+ - spec/spec_helper.rb
32
+ homepage: http://www.skzsr.com/
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.24
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Write a gem summary
56
+ test_files:
57
+ - spec/randomer_percent_spec.rb
58
+ - spec/randomer_ranger_spec.rb
59
+ - spec/spec_helper.rb