netrand 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ 3.0.0
2
+ ========
3
+ *Preformance, size and bug fixes
4
+ *Improvements in CLI (like... it works! ;)
5
+ *Massive changes in handling of file and sequence randomization
6
+ *Added Netrand.dice function
7
+ *Better documentation
8
+
1
9
  2.0.0
2
10
  ========
3
11
  *Added list order randomizer
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- netrand (2.0.0)
4
+ netrand (3.0.0)
5
5
 
6
6
  GEM
7
7
  specs:
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
- Wrapper around Random.org random number and string generator.
2
- Pretty much every parameter has maximum of 10 000.
3
- For more info look in the RDocs.
1
+ Wrapper around Random.org random number and string generator.
2
+ Pretty much every parameter has maximum of 10 000.
3
+ For more info look in the RDocs.
4
+ install using *sudo gem install Netrand*
5
+ Remember, that the tests only test the module, not Random.org service.
6
+ [Information about Random.org](http://www.random.org/faq/ "Random.org FAQ")
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
- require "Netrand"
3
-
4
- def self.loadstring
2
+ require "netrand"
3
+ def loadstring
5
4
  upperalpha = ARGV.include?("-ua") ? :upperalpha : nil
6
5
  loweralpha = ARGV.include?("-la") ? :loweralpha : nil
7
6
  digits = ARGV.include?("-d") ? :digits : nil
@@ -9,10 +8,43 @@ def self.loadstring
9
8
  Netrand.string(ARGV[1].to_i, ARGV[2].to_i, digits, upperalpha, loweralpha, unique)
10
9
  end
11
10
 
11
+ def invalid_args
12
+ puts "Invalid arguments! Stop."
13
+ exit 1
14
+ end
15
+
16
+ def loadint
17
+ if ARGV.length < 3 then invalid_args end
18
+ if ARGV.length < 4 and ARGV.include?("-u") then invalid_args end
19
+ if ARGV.include?("-u") #This must be first, so that we can process it independently from its position
20
+ uniq = :uniq
21
+ ARGV.delete("-u")
22
+ else
23
+ uniq = :notuniq
24
+ end
25
+ numget = ARGV[1].to_i
26
+ max = ARGV[2].to_i
27
+ if ARGV[3] == nil
28
+ min = 1
29
+ else
30
+ min = ARGV[3].to_i
31
+ end
32
+ Netrand.int(numget, max, min, uniq)
33
+ end
34
+
35
+ def loadsequence
36
+ if ARGV.length == 2
37
+ Netrand.sequence(ARGV[1].to_i, 1)
38
+ else
39
+ Netrand.sequence(ARGV[1].to_i, ARGV[2].to_i)
40
+ end
41
+ end
42
+
12
43
  def self.usage
13
- puts "netrand int|sequence|string|list"
44
+ puts "netrand int|sequence|string|file|dice"
14
45
  puts "You provide additional arguments according to documentation in ri Netrand"
15
46
  puts "You can force int to provide unique numbers by using -u flag at the end of command."
47
+ puts "file invokes randomize_file_lines function"
16
48
  puts "String mode flags:"
17
49
  puts ":upperalpha is activated using -ua switch"
18
50
  puts ":loweralpha using -la"
@@ -20,21 +52,32 @@ def self.usage
20
52
  puts ":unique using -u"
21
53
  end
22
54
 
55
+
56
+ def cmdout(arr)
57
+ arr.each do |e|
58
+ print e
59
+ print ", "
60
+ end
61
+ print "\n"
62
+ end
63
+
23
64
  case ARGV[0]
24
65
  when "int"
25
- out = Netrand.int(ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i, if ARGV[4] == "-u" then :unique end)
66
+ out = loadint
26
67
  when "sequence"
27
- out = Netrand.uniqint(ARGV[1].to_i, ARGV[2].to_i)
28
- when "list"
29
- out = Netrand.list(ARGV[1])
68
+ out = loadsequence
69
+ when "file"
70
+ out = Netrand.randomize_file_lines(ARGV[1])
30
71
  when "string"
31
- out = self.loadstring
72
+ out = loadstring
73
+ when "dice"
74
+ out = Netrand.dice(ARGV[1])
32
75
  else
33
76
  self.usage
34
77
  end
35
78
 
36
79
  if out.class == Array
37
- out.each {|i| print i + ","}
80
+ cmdout(out)
38
81
  else
39
82
  puts out
40
83
  end
@@ -0,0 +1,31 @@
1
+ require "open-uri"
2
+ module Netrand
3
+ #Generates exception if quota of random.org is exhausted.
4
+
5
+ def self.check_quota
6
+ quota = nil
7
+ open("http://www.random.org/quota/?format=plain") do |c|
8
+ quota = c.read.to_i
9
+ self.check_quota_value(quota)
10
+ end
11
+ quota
12
+ end
13
+
14
+ private
15
+
16
+ class QuotaExhaustedException < RuntimeError; end
17
+
18
+ #Harvests lines in plaintext output (private)
19
+ def self.harvest(address)
20
+ values = []
21
+ open(address) do |c|
22
+ c.each_line {|l| values << l.chop.to_i}
23
+ end
24
+ values
25
+ end
26
+
27
+ def self.check_quota_value(quota)
28
+ if quota < 0 then raise QuotaExhaustedException, "Random.org quota exhausted, please try again after 0:00 UTC" end
29
+ end
30
+
31
+ end
@@ -0,0 +1,10 @@
1
+ require_relative "int"
2
+ module Netrand
3
+ # Simulates dice rolls.
4
+ # Example: <br>
5
+ # Netrand.dice(10) <br>
6
+ # Can return array like this: [2, 4, 6, 3, 1, 5, 3, 2] <br>
7
+ def self.dice(dices)
8
+ self.int(dices, 6)
9
+ end
10
+ end
@@ -1,5 +1,4 @@
1
- require "open-uri"
2
- require_relative "quota"
1
+ require_relative "common"
3
2
  module Netrand
4
3
 
5
4
  #Generates numget random numbers using random.org service.
@@ -7,27 +6,26 @@ module Netrand
7
6
  #Returns Array of numbers.
8
7
  #If you use :unique flag, then there won't be two same numbers in the array.
9
8
 
10
- def Netrand.int(numget, max, min = 1, uniq = :notuniq)
11
- if numget == 0 then raise ArgumentError, "You must ask for more numbers than 0" end
12
- if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
13
- if min > max then raise ArgumentError, "max must be bigger than min!" end
9
+ def self.int(numget, max, min = 1, uniq = :notuniq)
10
+ raise ArgumentError, "You must ask for more numbers than 0" if numget == 0
11
+ raise ArgumentError, "Maximum number must be bigger than 0" if max == 0
12
+ raise ArgumentError, "max must be bigger than min!" if min > max
14
13
  self.check_quota
15
- values = self.load_random_integers(numget, max, min)
16
- if uniq == :unique
17
- values.uniq!
18
- while values.length != numget do
19
- values << self.load_random_integers(numget - values.length, max, min)
20
- values.uniq!
21
- end
22
- end
14
+ values = self.harvest("http://www.random.org/integers/?num=#{numget}&min=#{min}&max=#{max}&format=plain&col=1&base=10")
15
+ if uniq == :unique then self.get_unique_ints(numget, max, min, values) end
23
16
  values
24
17
  end
25
18
 
26
- def Netrand.load_random_integers(num, max, min)
27
- values = []
28
- open("http://www.random.org/integers/?num=#{num}&min=#{min}&max=#{max}&format=plain&col=1&base=10") do |c|
29
- c.each_line {|l| values << l.chop.to_i}
19
+ private
20
+
21
+ #Gets unique integers in a way that is fast and conserves bandwidth (private)
22
+ def self.get_unique_ints(numget, max, min, values = [])
23
+ values.uniq!
24
+ while values.length != numget do
25
+ values << self.harvest("http://www.random.org/integers/?num=#{numget - values.length}&min=#{min}&max=#{max}&format=plain&col=1&base=10")
26
+ values.uniq!
30
27
  end
31
28
  values
32
29
  end
30
+
33
31
  end
@@ -1,25 +1,39 @@
1
1
  require_relative "sequence"
2
2
  module Netrand
3
3
 
4
- #Writes lines from file into randomized_file
5
- #File must be shorter than 10000 lines
6
- def Netrand.list(file)
7
- src = File.new(file, "r")
8
- out = File.new("randomized_#{file}", "w")
9
- lines = countlines(file)
10
- rand = self.sequence(lines)
11
- rand.each do |pos|
12
- (pos-1).times {src.gets}
13
- line = src.gets
14
- out.puts(line)
15
- src.rewind
4
+ #Randomizes order of items in array
5
+ #Array must be shorter than 10 000 items
6
+ #Example: <br>
7
+ #Netrand.list([1, 2, 3]) <br>
8
+ #May return [2, 1, 3]
9
+
10
+ def self.list(arr)
11
+ randarr = []
12
+ random_positions = self.sequence(arr.length - 1, 0)
13
+ arr = arr.to_enum
14
+ random_positions.each do |pos|
15
+ randarr[pos] = arr.next
16
16
  end
17
+ randarr
18
+ end
19
+
20
+ #Randomizes order of lines in file
21
+ #They are saved to file called input_file_name-randomized.type
22
+
23
+ def self.randomize_file_lines(filename)
24
+ src = File.new(filename, "r")
25
+ out = File.new(assemble_randomized_path(src), "w")
26
+ values = src.readlines
27
+ randomized_values = self.list(values)
28
+ randomized_values.each {|value| out.write(value)}
17
29
  src.close
18
30
  out.close
19
31
  end
20
32
 
33
+ private
34
+
21
35
  #Counts lines in file
22
- def Netrand.countlines(file_to_count)
36
+ def self.countlines(file_to_count)
23
37
  count = 0
24
38
  File.open(file_to_count, "r") do |f|
25
39
  ret = 0
@@ -30,5 +44,19 @@ module Netrand
30
44
  end
31
45
  count
32
46
  end
47
+
48
+ #Assembles the filename, so that it won't corrupt paths
49
+ def self.assemble_randomized_path(input_file)
50
+ path = input_file.path
51
+ dir = File.dirname(path)
52
+
53
+ filename = path.split("/")[-1]
54
+ filename = filename.split(".")
55
+ filename = filename[0, filename.length - 1]
56
+
57
+ filetype = File.extname(path)
58
+ fn_with_suffix = filename[0] + "-randomized"
59
+ return dir + "/" + fn_with_suffix + filetype
60
+ end
33
61
 
34
62
  end
@@ -1,16 +1,11 @@
1
- require "open-uri"
2
- require_relative "quota"
1
+ require_relative "common"
3
2
  module Netrand
4
- #Returns numbers from min to max in random order.
5
3
 
6
- def Netrand.sequence(max, min = 1)
4
+ #Returns sequence of numbers from min to max in random order.
5
+ def self.sequence(max, min = 1)
7
6
  if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
8
7
  if min > max then raise ArgumentError, "max must be bigger than min!" end
9
8
  self.check_quota
10
- order = Array.new()
11
- open("http://www.random.org/sequences/?min=#{min}&max=#{max}&col=1&format=plain&rnd=new") do |c|
12
- c.each_line {|l| order << l.chop.to_i}
13
- end
14
- order
9
+ order = self.harvest("http://www.random.org/sequences/?min=#{min}&max=#{max}&col=1&format=plain&rnd=new")
15
10
  end
16
11
  end
@@ -1,12 +1,15 @@
1
- require "open-uri"
2
- require_relative "quota"
1
+ require_relative "common"
3
2
  module Netrand
4
3
 
5
4
  #Generates num random strings of len length.
6
5
  #They can contain digits, uppercase and lowercase letters.
7
6
  #They can be set to be unique.
7
+ #Example:<br>
8
+ #Netrand.string(10, 5, :upperalpha, :loweralpha, :digits, :unique) <br>
9
+ #Will produce 10 5 characters long strings containing uppercase, lowercase letters and digits.
10
+ #There won't be 2 same characters in one string. This example used all of the possible options
8
11
 
9
- def Netrand.string(num, len, *opts)
12
+ def self.string(num, len, *opts)
10
13
  digits = opts.include?(:digits) ? "on" : "off"
11
14
  upperalpha = opts.include?(:upperalpha) ? "on" : "off"
12
15
  loweralpha = opts.include?(:loweralpha) ? "on" : "off"
@@ -1,4 +1,6 @@
1
+ require_relative "functions/common"
1
2
  require_relative "functions/string"
2
3
  require_relative "functions/sequence"
3
4
  require_relative "functions/int"
4
- require_relative "functions/list"
5
+ require_relative "functions/list"
6
+ require_relative "functions/dice"
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
2
 
3
3
  Gem::Specification.new do |s|
4
- s.name = "netrand"
5
- s.summary = "Generate true random numbers using random.org"
6
- s.description= "Wrapper around Random.org random number and string generator."
7
- s.requirements = [ 'Working internet connection' ]
8
- s.version = "2.0.0"
4
+ s.name = "netrand"
5
+ s.summary = "Generate true random numbers using random.org"
6
+ s.description = "Wrapper around Random.org random number and string generator."
7
+ s.requirements= [ 'Working internet connection' ]
8
+ s.version = "3.0.0"
9
9
  s.author = "Róbert Selvek"
10
10
  s.email = "me@sellweek.eu"
11
11
  s.homepage = "http://github.com/sellweek/netrand"
Binary file
@@ -0,0 +1,79 @@
1
+ Svidník
2
+ Skalica
3
+ Detva
4
+ Dolný Kubín
5
+ Komárno
6
+ Šaľa
7
+ Zlaté Moravce
8
+ Bratislava V
9
+ Trebišov
10
+ Malacky
11
+ Nové Mesto nad Váhom
12
+ Partizánske
13
+ Bratislava I
14
+ Žilina
15
+ Piešťany
16
+ Lučenec
17
+ Košice II
18
+ Levice
19
+ Snina
20
+ Hlohovec
21
+ Zvolen
22
+ Prešov
23
+ Bratislava IV
24
+ Pezinok
25
+ Dunajská Streda
26
+ Myjava
27
+ Liptovský Mikuláš
28
+ Bratislava II
29
+ Košice III
30
+ Ružomberok
31
+ Martin
32
+ Kysucké Nové Mesto
33
+ Čadca
34
+ Sobrance
35
+ Poprad
36
+ Kežmarok
37
+ Stará Ľubovňa
38
+ Gelnica
39
+ Michalovce
40
+ Humenné
41
+ Trenčín
42
+ Krupina
43
+ Banská Bystrica
44
+ Tvrdošín
45
+ Nitra
46
+ Sabinov
47
+ Košice-okolie
48
+ Nové Zámky
49
+ Trnava
50
+ Poltár
51
+ Púchov
52
+ Brezno
53
+ Veľký Krtíš
54
+ Senica
55
+ Levoča
56
+ Topoľčany
57
+ Bardejov
58
+ Medzilaborce
59
+ Galanta
60
+ Ilava
61
+ Košice IV
62
+ Bytča
63
+ Spišská Nová Ves
64
+ Prievidza
65
+ Bratislava III
66
+ Košice I
67
+ Považská Bystrica
68
+ Žiar nad Hronom
69
+ Rimavská Sobota
70
+ Žarnovica
71
+ Bánovce nad Bebravou
72
+ Turčianske Teplice
73
+ Vranov nad Topľou
74
+ Stropkov
75
+ Revúca
76
+ Senec
77
+ Námestovo
78
+ Banská Štiavnica
79
+ Rožňava
@@ -76,4 +76,4 @@ Zlaté Moravce
76
76
  Zvolen
77
77
  Žarnovica
78
78
  Žiar nad Hronom
79
- Žilina
79
+ Žilina
@@ -1,5 +1,5 @@
1
- puts "The Netrand.list tests are going to fail. Move them to folder you have write rights to and then try it."
2
1
  require_relative "test_int"
3
2
  require_relative "test_sequence"
4
3
  require_relative "test_string"
5
- require_relative "test_list"
4
+ require_relative "test_list"
5
+ require_relative "test_dice"
@@ -0,0 +1,7 @@
1
+ require "netrand"
2
+ require "test/unit"
3
+ class TestDice < MiniTest::Unit::TestCase
4
+ def test_real_dice
5
+ Netrand.dice(20).each {|n| assert n<=6}
6
+ end
7
+ end
@@ -1,5 +1,4 @@
1
- require "Netrand"
2
-
1
+ require "netrand"
3
2
  require "test/unit"
4
3
  class TestInt < MiniTest::Unit::TestCase
5
4
 
@@ -1,20 +1,21 @@
1
- require "Netrand"
2
-
1
+ require "netrand"
3
2
  require "test/unit"
4
3
  class TestList < MiniTest::Unit::TestCase
5
- def test_randomization
4
+ def setup
5
+ Netrand.randomize_file_lines("#{File.dirname(__FILE__)}/ZoznamOkresov.txt")
6
+ end
7
+ def test_file_randomization
6
8
  original = Array.new
7
9
  randomized = Array.new
8
10
  line = 0
9
- File.open("ZoznamOkresov.txt", "r") do |f|
11
+ File.open("#{File.dirname(__FILE__)}/ZoznamOkresov.txt", "r") do |f|
10
12
  until line == nil
11
13
  line = f.gets
12
14
  original << line
13
15
  end
14
16
  end
15
17
 
16
- Netrand.list("ZoznamOkresov.txt")
17
- File.open("randomized_ZoznamOkresov.txt", "r") do |f|
18
+ File.open("#{File.dirname(__FILE__)}/ZoznamOkresov-randomized.txt", "r") do |f|
18
19
  until line == nil
19
20
  line = f.gets
20
21
  randomized << line
@@ -23,20 +24,18 @@ class TestList < MiniTest::Unit::TestCase
23
24
  refute_equal(randomized, original)
24
25
  end
25
26
 
26
- def test_not_same
27
+ def test_file_not_same
27
28
  rand1 = Array.new
28
29
  rand2 = Array.new
29
30
  line = 0
30
- Netrand.list("ZoznamOkresov.txt")
31
- File.open("randomized_ZoznamOkresov.txt", "r") do |f|
31
+ File.open("#{File.dirname(__FILE__)}/ZoznamOkresov-randomized.txt", "r") do |f|
32
32
  until line == nil
33
33
  line = f.gets
34
34
  rand1 << line
35
35
  end
36
36
  end
37
37
 
38
- Netrand.list("ZoznamOkresov.txt")
39
- File.open("randomized_ZoznamOkresov.txt", "r") do |f|
38
+ File.open("#{File.dirname(__FILE__)}/ZoznamOkresov-randomized.txt", "r") do |f|
40
39
  until line == nil
41
40
  line = f.gets
42
41
  rand2 << line
@@ -1,5 +1,4 @@
1
- require "Netrand"
2
-
1
+ require "netrand"
3
2
  require "test/unit"
4
3
  class TestSequence < MiniTest::Unit::TestCase
5
4
  def test_randomization
@@ -1,5 +1,4 @@
1
- require "Netrand"
2
-
1
+ require "netrand"
3
2
  require "test/unit"
4
3
  class TestString < MiniTest::Unit::TestCase
5
4
  def test_not_same
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: netrand
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.0.0
5
+ version: 3.0.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - "R\xC3\xB3bert Selvek"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-28 00:00:00 Z
13
+ date: 2011-07-12 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Wrapper around Random.org random number and string generator.
@@ -26,21 +26,25 @@ files:
26
26
  - CHANGELOG
27
27
  - Gemfile
28
28
  - Gemfile.lock
29
+ - lib/functions/common.rb
30
+ - lib/functions/dice.rb
29
31
  - lib/functions/int.rb
30
32
  - lib/functions/list.rb
31
- - lib/functions/quota.rb
32
33
  - lib/functions/sequence.rb
33
34
  - lib/functions/string.rb
34
35
  - lib/netrand.rb
35
36
  - LICENSE
36
37
  - netrand.gemspec
38
+ - pkg/netrand-3.0.0.gem
37
39
  - Rakefile
38
40
  - README.md
39
41
  - tests/test.rb
42
+ - tests/test_dice.rb
40
43
  - tests/test_int.rb
41
44
  - tests/test_list.rb
42
45
  - tests/test_sequence.rb
43
46
  - tests/test_string.rb
47
+ - tests/ZoznamOkresov-randomized.txt
44
48
  - tests/ZoznamOkresov.txt
45
49
  homepage: http://github.com/sellweek/netrand
46
50
  licenses: []
@@ -61,14 +65,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
65
  requirements:
62
66
  - - ">="
63
67
  - !ruby/object:Gem::Version
64
- hash: 1609978401844798295
68
+ hash: 1909816175611674823
65
69
  segments:
66
70
  - 0
67
71
  version: "0"
68
72
  requirements:
69
73
  - Working internet connection
70
74
  rubyforge_project:
71
- rubygems_version: 1.7.2
75
+ rubygems_version: 1.8.5
72
76
  signing_key:
73
77
  specification_version: 3
74
78
  summary: Generate true random numbers using random.org
@@ -1,16 +0,0 @@
1
- require "open-uri"
2
- require_relative "quota"
3
- module Netrand
4
- #Generates exception if quota of random.org is exhausted.
5
-
6
- def Netrand.check_quota
7
- quota = nil
8
- open("http://www.random.org/quota/?format=plain") do |c|
9
- quota = c.read.to_i
10
- if quota < 0 then raise QuotaExhaustedException, "Random.org quota exhausted, please try again after 0:00 UTC" end
11
- end
12
- quota
13
- end
14
-
15
- class QuotaExhaustedException < RuntimeError; end
16
- end