netrand 1.0.1 → 2.0.0

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/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 2.0.0
2
+ ========
3
+ *Added list order randomizer
4
+ *Changed the name of *Netrand.order* to *Netrand.sequence* to correspond with official random.org documentation
5
+ *Some internal changes to make the code clearer
6
+
1
7
  1.0.1
2
8
  ======
3
9
  Changed package description. Stupid RubyGems.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- netrand (1.0.1)
4
+ netrand (2.0.0)
5
5
 
6
6
  GEM
7
7
  specs:
data/README.md CHANGED
@@ -1,2 +1,3 @@
1
1
  Wrapper around Random.org random number and string generator.
2
- For more info look in the RDocs
2
+ Pretty much every parameter has maximum of 10 000.
3
+ For more info look in the RDocs.
data/bin/netrand CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
- require_relative "../lib/netrand.rb"
2
+ require "Netrand"
3
+
3
4
  def self.loadstring
4
5
  upperalpha = ARGV.include?("-ua") ? :upperalpha : nil
5
6
  loweralpha = ARGV.include?("-la") ? :loweralpha : nil
@@ -9,10 +10,10 @@ def self.loadstring
9
10
  end
10
11
 
11
12
  def self.usage
12
- puts "netrand int|uniqint|order|string"
13
+ puts "netrand int|sequence|string|list"
13
14
  puts "You provide additional arguments according to documentation in ri Netrand"
14
15
  puts "You can force int to provide unique numbers by using -u flag at the end of command."
15
- puts "Only in string mode, there are some changes."
16
+ puts "String mode flags:"
16
17
  puts ":upperalpha is activated using -ua switch"
17
18
  puts ":loweralpha using -la"
18
19
  puts ":digits using -d"
@@ -21,14 +22,19 @@ end
21
22
 
22
23
  case ARGV[0]
23
24
  when "int"
24
- puts Netrand.int(ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i, if ARGV[4] == "-u" then :unique end)
25
- when "uniqint"
26
- puts Netrand.uniqint(ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i)
27
- when "order"
28
- puts Netrand.uniqint(ARGV[1].to_i, ARGV[2].to_i)
25
+ out = Netrand.int(ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i, if ARGV[4] == "-u" then :unique end)
26
+ when "sequence"
27
+ out = Netrand.uniqint(ARGV[1].to_i, ARGV[2].to_i)
28
+ when "list"
29
+ out = Netrand.list(ARGV[1])
29
30
  when "string"
30
- puts self.loadstring
31
+ out = self.loadstring
31
32
  else
32
33
  self.usage
33
34
  end
34
35
 
36
+ if out.class == Array
37
+ out.each {|i| print i + ","}
38
+ else
39
+ puts out
40
+ end
@@ -0,0 +1,33 @@
1
+ require "open-uri"
2
+ require_relative "quota"
3
+ module Netrand
4
+
5
+ #Generates numget random numbers using random.org service.
6
+ #You must set maximum and can set minimum number.
7
+ #Returns Array of numbers.
8
+ #If you use :unique flag, then there won't be two same numbers in the array.
9
+
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
14
+ 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
23
+ values
24
+ end
25
+
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}
30
+ end
31
+ values
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ require_relative "sequence"
2
+ module Netrand
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
16
+ end
17
+ src.close
18
+ out.close
19
+ end
20
+
21
+ #Counts lines in file
22
+ def Netrand.countlines(file_to_count)
23
+ count = 0
24
+ File.open(file_to_count, "r") do |f|
25
+ ret = 0
26
+ until ret == nil
27
+ ret = f.gets
28
+ count += 1
29
+ end
30
+ end
31
+ count
32
+ end
33
+
34
+ end
@@ -0,0 +1,16 @@
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
@@ -0,0 +1,16 @@
1
+ require "open-uri"
2
+ require_relative "quota"
3
+ module Netrand
4
+ #Returns numbers from min to max in random order.
5
+
6
+ def Netrand.sequence(max, min = 1)
7
+ if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
8
+ if min > max then raise ArgumentError, "max must be bigger than min!" end
9
+ 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
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ require "open-uri"
2
+ require_relative "quota"
3
+ module Netrand
4
+
5
+ #Generates num random strings of len length.
6
+ #They can contain digits, uppercase and lowercase letters.
7
+ #They can be set to be unique.
8
+
9
+ def Netrand.string(num, len, *opts)
10
+ digits = opts.include?(:digits) ? "on" : "off"
11
+ upperalpha = opts.include?(:upperalpha) ? "on" : "off"
12
+ loweralpha = opts.include?(:loweralpha) ? "on" : "off"
13
+ unique = opts.include?(:unique) ? "on" : "off"
14
+ if len == 0 then raise ArgumentError, "Strings must be longer than 0 characters." end
15
+ if digits == "off" && upperalpha == "off" && loweralpha == "off" then raise ArgumentError, "You disabled every type of character." end
16
+ values = Array.new
17
+ self.check_quota
18
+ open("http://www.random.org/strings/?num=#{num}&len=#{len}&digits=#{digits}&upperalpha=#{upperalpha}&loweralpha=#{loweralpha}&unique=#{unique}&format=plain") do |c|
19
+ c.each_line {|l| values << l.chop}
20
+ end
21
+ values
22
+ end
23
+ end
data/lib/netrand.rb CHANGED
@@ -1,77 +1,4 @@
1
- require "open-uri"
2
- module Netrand
3
-
4
- #Generates numget random numbers using random.org service.
5
- #You must set maximum and can set minimum number.
6
- #Returns Array of numbers.
7
- #If you use :unique flag, then there won't be two same numbers in the array.
8
-
9
- def Netrand.int(numget, max, min = 1, uniq = :notuniq)
10
- if numget == 0 then raise ArgumentError, "You must ask for more numbers than 0" end
11
- if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
12
- if min > max then raise ArgumentError, "max must be bigger than min!" end
13
- values = Array.new
14
- random = lambda do |num, max, min|
15
- open("http://www.random.org/integers/?num=#{num}&min=#{min}&max=#{max}&format=plain&col=1&base=10") do |c|
16
- c.each_line {|l| values << l.chop.to_i}
17
- end
18
- end
19
- self.check_quota
20
- random.call(numget, max, min)
21
- if uniq == :unique
22
- values.uniq!
23
- while values.length != numget do
24
- random.call(numget - values.length, max, min)
25
- values.uniq!
26
- end
27
- end
28
- values
29
- end
30
-
31
- #Returns numbers from min to max in random order.
32
-
33
- def Netrand.order(max, min = 1)
34
- if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
35
- if min > max then raise ArgumentError, "max must be bigger than min!" end
36
- self.check_quota
37
- order = Array.new()
38
- open("http://www.random.org/sequences/?min=#{min}&max=#{max}&col=1&format=plain&rnd=new") do |c|
39
- c.each_line {|l| order << l.chop.to_i}
40
- end
41
- order
42
- end
43
-
44
- #Generates num random strings of len length.
45
- #They can contain digits, uppercase and lowercase letters.
46
- #They can be set to be unique.
47
-
48
- def Netrand.string(num, len, *opts)
49
- digits = opts.include?(:digits) ? "on" : "off"
50
- upperalpha = opts.include?(:upperalpha) ? "on" : "off"
51
- loweralpha = opts.include?(:loweralpha) ? "on" : "off"
52
- unique = opts.include?(:unique) ? "on" : "off"
53
- if len == 0 then raise ArgumentError, "Strings must be longer than 0 characters." end
54
- if digits == "off" && upperalpha == "off" && loweralpha == "off" then raise ArgumentError, "You disabled every type of character." end
55
- values = Array.new
56
- self.check_quota
57
- open("http://www.random.org/strings/?num=#{num}&len=#{len}&digits=#{digits}&upperalpha=#{upperalpha}&loweralpha=#{loweralpha}&unique=#{unique}&format=plain") do |c|
58
- c.each_line {|l| values << l.chop}
59
- end
60
- values
61
- end
62
-
63
- #Generates exception if quota of random.org is exhausted.
64
-
65
- def Netrand.check_quota
66
- quota = nil
67
- open("http://www.random.org/quota/?format=plain") do |c|
68
- quota = c.read.to_i
69
- if quota < 0 then raise QuotaExhaustedException, "Random.org quota exhausted, please try again after 0:00 UTC" end
70
- end
71
- quota
72
- end
73
-
74
- class QuotaExhaustedException < RuntimeError
75
- end
76
-
77
- end
1
+ require_relative "functions/string"
2
+ require_relative "functions/sequence"
3
+ require_relative "functions/int"
4
+ require_relative "functions/list"
data/netrand.gemspec CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
5
5
  s.summary = "Generate true random numbers using random.org"
6
6
  s.description= "Wrapper around Random.org random number and string generator."
7
7
  s.requirements = [ 'Working internet connection' ]
8
- s.version = "1.0.1"
8
+ s.version = "2.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"
@@ -0,0 +1,79 @@
1
+ Banská Bystrica
2
+ Banská Štiavnica
3
+ Bardejov
4
+ Bánovce nad Bebravou
5
+ Brezno
6
+ Bratislava I
7
+ Bratislava II
8
+ Bratislava III
9
+ Bratislava IV
10
+ Bratislava V
11
+ Bytča
12
+ Čadca
13
+ Detva
14
+ Dolný Kubín
15
+ Dunajská Streda
16
+ Galanta
17
+ Gelnica
18
+ Hlohovec
19
+ Humenné
20
+ Ilava
21
+ Kežmarok
22
+ Komárno
23
+ Košice I
24
+ Košice II
25
+ Košice III
26
+ Košice IV
27
+ Košice-okolie
28
+ Krupina
29
+ Kysucké Nové Mesto
30
+ Levice
31
+ Levoča
32
+ Liptovský Mikuláš
33
+ Lučenec
34
+ Malacky
35
+ Martin
36
+ Medzilaborce
37
+ Michalovce
38
+ Myjava
39
+ Námestovo
40
+ Nitra
41
+ Nové Mesto nad Váhom
42
+ Nové Zámky
43
+ Partizánske
44
+ Pezinok
45
+ Piešťany
46
+ Poltár
47
+ Poprad
48
+ Považská Bystrica
49
+ Prešov
50
+ Prievidza
51
+ Púchov
52
+ Revúca
53
+ Rimavská Sobota
54
+ Rožňava
55
+ Ružomberok
56
+ Sabinov
57
+ Senec
58
+ Senica
59
+ Skalica
60
+ Snina
61
+ Sobrance
62
+ Spišská Nová Ves
63
+ Stará Ľubovňa
64
+ Stropkov
65
+ Svidník
66
+ Šaľa
67
+ Topoľčany
68
+ Trebišov
69
+ Trenčín
70
+ Trnava
71
+ Turčianske Teplice
72
+ Tvrdošín
73
+ Veľký Krtíš
74
+ Vranov nad Topľou
75
+ Zlaté Moravce
76
+ Zvolen
77
+ Žarnovica
78
+ Žiar nad Hronom
79
+ Žilina
data/tests/test.rb CHANGED
@@ -1,4 +1,5 @@
1
- puts "WARNING: This can take looong time!"
2
- require_relative "Testint"
3
- require_relative "Testorder"
4
- require_relative "Teststring"
1
+ puts "The Netrand.list tests are going to fail. Move them to folder you have write rights to and then try it."
2
+ require_relative "test_int"
3
+ require_relative "test_sequence"
4
+ require_relative "test_string"
5
+ require_relative "test_list"
@@ -1,4 +1,5 @@
1
- require_relative "../lib/netrand.rb"
1
+ require "Netrand"
2
+
2
3
  require "test/unit"
3
4
  class TestInt < MiniTest::Unit::TestCase
4
5
 
@@ -0,0 +1,49 @@
1
+ require "Netrand"
2
+
3
+ require "test/unit"
4
+ class TestList < MiniTest::Unit::TestCase
5
+ def test_randomization
6
+ original = Array.new
7
+ randomized = Array.new
8
+ line = 0
9
+ File.open("ZoznamOkresov.txt", "r") do |f|
10
+ until line == nil
11
+ line = f.gets
12
+ original << line
13
+ end
14
+ end
15
+
16
+ Netrand.list("ZoznamOkresov.txt")
17
+ File.open("randomized_ZoznamOkresov.txt", "r") do |f|
18
+ until line == nil
19
+ line = f.gets
20
+ randomized << line
21
+ end
22
+ end
23
+ refute_equal(randomized, original)
24
+ end
25
+
26
+ def test_not_same
27
+ rand1 = Array.new
28
+ rand2 = Array.new
29
+ line = 0
30
+ Netrand.list("ZoznamOkresov.txt")
31
+ File.open("randomized_ZoznamOkresov.txt", "r") do |f|
32
+ until line == nil
33
+ line = f.gets
34
+ rand1 << line
35
+ end
36
+ end
37
+
38
+ Netrand.list("ZoznamOkresov.txt")
39
+ File.open("randomized_ZoznamOkresov.txt", "r") do |f|
40
+ until line == nil
41
+ line = f.gets
42
+ rand2 << line
43
+ end
44
+ end
45
+ refute_equal(rand1, rand2)
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,21 @@
1
+ require "Netrand"
2
+
3
+ require "test/unit"
4
+ class TestSequence < MiniTest::Unit::TestCase
5
+ def test_randomization
6
+ ref = (1..953).to_a
7
+ refute_equal(ref, Netrand.sequence(953, 1))
8
+ end
9
+
10
+ def test_not_same
11
+ nums1 = Netrand.sequence(10)
12
+ nums2 = Netrand.sequence(10)
13
+ refute_equal(nums1, nums2)
14
+ end
15
+
16
+ def test_error_handling
17
+ assert_raises(ArgumentError) {Netrand.sequence(3, 10)}
18
+ assert_raises(ArgumentError) {Netrand.sequence(0)}
19
+ end
20
+
21
+ end
@@ -1,4 +1,5 @@
1
- require_relative "../lib/netrand.rb"
1
+ require "Netrand"
2
+
2
3
  require "test/unit"
3
4
  class TestString < MiniTest::Unit::TestCase
4
5
  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: 1.0.1
5
+ version: 2.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-23 00:00:00 Z
13
+ date: 2011-04-28 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Wrapper around Random.org random number and string generator.
@@ -24,18 +24,24 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - bin/netrand
26
26
  - CHANGELOG
27
- - Examples/random.rb
28
27
  - Gemfile
29
28
  - Gemfile.lock
29
+ - lib/functions/int.rb
30
+ - lib/functions/list.rb
31
+ - lib/functions/quota.rb
32
+ - lib/functions/sequence.rb
33
+ - lib/functions/string.rb
30
34
  - lib/netrand.rb
31
35
  - LICENSE
32
36
  - netrand.gemspec
33
37
  - Rakefile
34
38
  - README.md
35
39
  - tests/test.rb
36
- - tests/Testint.rb
37
- - tests/Testorder.rb
38
- - tests/Teststring.rb
40
+ - tests/test_int.rb
41
+ - tests/test_list.rb
42
+ - tests/test_sequence.rb
43
+ - tests/test_string.rb
44
+ - tests/ZoznamOkresov.txt
39
45
  homepage: http://github.com/sellweek/netrand
40
46
  licenses: []
41
47
 
@@ -55,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
61
  requirements:
56
62
  - - ">="
57
63
  - !ruby/object:Gem::Version
58
- hash: -1058495667687168366
64
+ hash: 1609978401844798295
59
65
  segments:
60
66
  - 0
61
67
  version: "0"
data/Examples/random.rb DELETED
@@ -1,2 +0,0 @@
1
- require_relative "../lib/netrand.rb"
2
- print Netrand.uniqint(100, 10000)
data/tests/Testorder.rb DELETED
@@ -1,20 +0,0 @@
1
- require_relative "../lib/netrand.rb"
2
- require "test/unit"
3
- class Testorder < MiniTest::Unit::TestCase
4
- def test_randomization
5
- ref = (1..953).to_a
6
- refute_equal(ref, Netrand.order(953, 1))
7
- end
8
-
9
- def test_not_same
10
- nums1 = Netrand.order(10)
11
- nums2 = Netrand.order(10)
12
- refute_equal(nums1, nums2)
13
- end
14
-
15
- def test_error_handling
16
- assert_raises(ArgumentError) {Netrand.order(3, 10)}
17
- assert_raises(ArgumentError) {Netrand.order(0)}
18
- end
19
-
20
- end