netrand 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/Examples/random.rb +2 -0
- data/LICENSE +19 -0
- data/README.md +18 -0
- data/Rakefile +21 -0
- data/bin/netrand +18 -0
- data/lib/netrand.rb +68 -0
- data/tests/Testint.rb +17 -0
- data/tests/Testorder.rb +20 -0
- data/tests/TestuniqInt.rb +23 -0
- data/tests/test.rb +4 -0
- metadata +81 -0
data/Examples/random.rb
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2011 by Róbert Selvek
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
API
|
2
|
+
==========
|
3
|
+
|
4
|
+
Netrand.uniqint(numget, max, min = 0)
|
5
|
+
------------------------------------------
|
6
|
+
Generates *numget* random numbers using random.org service. You must set maximum and can set minimum number. Returns Array of numbers in which there won't be two same numbers.
|
7
|
+
|
8
|
+
Netrand.int(numget, max, min = 0)
|
9
|
+
------------------------------------------
|
10
|
+
Generates *numget* random numbers using random.org service. You must set maximum and can set minimum number. Returns Array of numbers.
|
11
|
+
|
12
|
+
Netrand.order(max, min = 1)
|
13
|
+
-----------------------------------------
|
14
|
+
Returns numbers from *min* to *max* in random order.
|
15
|
+
|
16
|
+
Netrand.check_quota
|
17
|
+
------------------------
|
18
|
+
Generates exception if quota of random.org is exhausted.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
|
4
|
+
spec = Gem::Specification.new do |s|
|
5
|
+
s.name = "netrand"
|
6
|
+
s.summary = "Generate true random numbers using random.org"
|
7
|
+
s.description= File.read(File.join(File.dirname(__FILE__), 'README.md'))
|
8
|
+
s.requirements =
|
9
|
+
[ 'Working internet connection' ]
|
10
|
+
s.version = "0.0.1"
|
11
|
+
s.author = "Róbert Selvek"
|
12
|
+
s.email = "me@sellweek.eu"
|
13
|
+
s.platform = Gem::Platform::RUBY
|
14
|
+
s.required_ruby_version = '>=1.9'
|
15
|
+
s.files = Dir['**/**']
|
16
|
+
s.executables = [ 'netrand' ]
|
17
|
+
s.test_files = Dir["test/test*.rb"]
|
18
|
+
s.has_rdoc = true
|
19
|
+
end
|
20
|
+
|
21
|
+
Rake::GemPackageTask.new(spec).define
|
data/bin/netrand
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "netrand"
|
3
|
+
case ARGV[0]
|
4
|
+
when "int"
|
5
|
+
puts Netrand.int(ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i)
|
6
|
+
when "uniqint"
|
7
|
+
puts Netrand.uniqint(ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i)
|
8
|
+
when "order"
|
9
|
+
puts Netrand.uniqint(ARGV[1].to_i, ARGV[2].to_i)
|
10
|
+
else
|
11
|
+
usage
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
def usage()
|
17
|
+
#put up usage banner
|
18
|
+
end
|
data/lib/netrand.rb
ADDED
@@ -0,0 +1,68 @@
|
|
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
|
+
|
8
|
+
def Netrand.int(numget, max, min = 1)
|
9
|
+
if numget == 0 then raise ArgumentError, "You must ask for more numbers than 0" end
|
10
|
+
if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
|
11
|
+
if min > max then raise ArgumentError, "max must be bigger than min!" end
|
12
|
+
values = Array.new
|
13
|
+
self.check_quota
|
14
|
+
open("http://www.random.org/integers/?num=#{numget}&min=#{min}&max=#{max}&format=plain&col=1&base=10") do |c|
|
15
|
+
c.each_line {|l| values << l.chop.to_i}
|
16
|
+
end
|
17
|
+
values
|
18
|
+
end
|
19
|
+
|
20
|
+
#Generates numget random numbers using random.org service.
|
21
|
+
#You must set maximum and can set minimum number.
|
22
|
+
#Returns Array of numbers in which there won't be two same numbers.
|
23
|
+
|
24
|
+
def Netrand.uniqint(numget, max, min = 1)
|
25
|
+
if numget == 0 then raise ArgumentError, "You must ask for more numbers than 0" end
|
26
|
+
if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
|
27
|
+
if min > max then raise ArgumentError, "max must be bigger than min!" end
|
28
|
+
values = Array.new
|
29
|
+
random = lambda do |num, max, min|
|
30
|
+
open("http://www.random.org/integers/?num=#{num}&min=#{min}&max=#{max}&format=plain&col=1&base=10") do |c|
|
31
|
+
c.each_line {|l| values << l.chop.to_i}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
self.check_quota
|
35
|
+
random.call(numget, max, min)
|
36
|
+
values.uniq!
|
37
|
+
while values.length != numget do
|
38
|
+
random.call(numget - values.length, max, min)
|
39
|
+
values.uniq!
|
40
|
+
end
|
41
|
+
values
|
42
|
+
end
|
43
|
+
|
44
|
+
#Returns numbers from min to max in random order.
|
45
|
+
|
46
|
+
def Netrand.order(max, min = 1)
|
47
|
+
if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
|
48
|
+
if min > max then raise ArgumentError, "max must be bigger than min!" end
|
49
|
+
self.check_quota
|
50
|
+
order = Array.new()
|
51
|
+
open("http://www.random.org/sequences/?min=#{min}&max=#{max}&col=1&format=plain&rnd=new") do |c|
|
52
|
+
c.each_line {|l| order << l.chop.to_i}
|
53
|
+
end
|
54
|
+
order
|
55
|
+
end
|
56
|
+
|
57
|
+
#Generates exception if quota of random.org is exhausted.
|
58
|
+
|
59
|
+
def Netrand.check_quota
|
60
|
+
open("http://www.random.org/quota/?format=plain") do |c|
|
61
|
+
if c.read.to_i < 0 then raise QuotaExhaustedException, "Random.org quota exhausted, please try again after 0:00 UTC" end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class QuotaExhaustedException < RuntimeError
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/tests/Testint.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "netrand"
|
2
|
+
require "test/unit"
|
3
|
+
class Testint < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def test_not_same
|
6
|
+
nums1 = Netrand.int(3, 100)
|
7
|
+
nums2 = Netrand.int(3, 100)
|
8
|
+
refute_equal(nums1, nums2)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_error_handling
|
12
|
+
assert_raises(ArgumentError) {Netrand.int(0, 10)}
|
13
|
+
assert_raises(ArgumentError) {Netrand.int(100, 0)}
|
14
|
+
assert_raises(ArgumentError) {Netrand.int(100, 999999, 9999991)}
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/tests/Testorder.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "netrand"
|
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
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "netrand"
|
2
|
+
require "test/unit"
|
3
|
+
class TestInt < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def test_uniq
|
6
|
+
nums = Netrand.uniqint(150, 10000)
|
7
|
+
assert_equal(nums.uniq.length, nums.length)
|
8
|
+
assert_equal(150, nums.length)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_not_same
|
12
|
+
nums1 = Netrand.uniqint(3, 100)
|
13
|
+
nums2 = Netrand.uniqint(3, 100)
|
14
|
+
refute_equal(nums1, nums2)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_error_handling
|
18
|
+
assert_raises(ArgumentError) {Netrand.uniqint(0, 10)}
|
19
|
+
assert_raises(ArgumentError) {Netrand.uniqint(100, 0)}
|
20
|
+
assert_raises(ArgumentError) {Netrand.uniqint(100, 999999, 9999991)}
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/tests/test.rb
ADDED
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: netrand
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- "R\xC3\xB3bert Selvek"
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-22 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: |-
|
17
|
+
API
|
18
|
+
==========
|
19
|
+
|
20
|
+
Netrand.uniqint(numget, max, min = 0)
|
21
|
+
------------------------------------------
|
22
|
+
Generates *numget* random numbers using random.org service. You must set maximum and can set minimum number. Returns Array of numbers in which there won't be two same numbers.
|
23
|
+
|
24
|
+
Netrand.int(numget, max, min = 0)
|
25
|
+
------------------------------------------
|
26
|
+
Generates *numget* random numbers using random.org service. You must set maximum and can set minimum number. Returns Array of numbers.
|
27
|
+
|
28
|
+
Netrand.order(max, min = 1)
|
29
|
+
-----------------------------------------
|
30
|
+
Returns numbers from *min* to *max* in random order.
|
31
|
+
|
32
|
+
Netrand.check_quota
|
33
|
+
------------------------
|
34
|
+
Generates exception if quota of random.org is exhausted.
|
35
|
+
email: me@sellweek.eu
|
36
|
+
executables:
|
37
|
+
- netrand
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
files:
|
43
|
+
- bin/netrand
|
44
|
+
- Examples/random.rb
|
45
|
+
- lib/netrand.rb
|
46
|
+
- LICENSE
|
47
|
+
- Rakefile
|
48
|
+
- README.md
|
49
|
+
- tests/test.rb
|
50
|
+
- tests/Testint.rb
|
51
|
+
- tests/Testorder.rb
|
52
|
+
- tests/TestuniqInt.rb
|
53
|
+
homepage:
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "1.9"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
requirements:
|
74
|
+
- Working internet connection
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.7.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Generate true random numbers using random.org
|
80
|
+
test_files: []
|
81
|
+
|