realrand 1.0.2

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.
@@ -0,0 +1,132 @@
1
+ # = RealRand
2
+ #
3
+ # Author:: Maik Schmidt <contact@maik-schmidt.de>
4
+ # Copyright:: Copyright (c) 2003 Maik Schmidt
5
+ # License:: Distributes under the same terms as Ruby.
6
+ #
7
+
8
+ require 'net/http'
9
+
10
+ module Random
11
+ class OnlineGenerator
12
+ attr_reader :host
13
+ attr_accessor :proxy_host, :proxy_port, :proxy_usr, :proxy_pwd
14
+
15
+ def initialize(host)
16
+ @host = host
17
+ @proxy_host = nil
18
+ @proxy_port = -1
19
+ @proxy_usr = nil
20
+ @proxy_pwd = nil
21
+ end
22
+
23
+ protected
24
+
25
+ def get_response(script, parameters)
26
+ Net::HTTP::Proxy(
27
+ @proxy_host,
28
+ @proxy_port,
29
+ @proxy_usr,
30
+ @proxy_pwd
31
+ ).start(@host) { |h|
32
+ response = h.get("#{script}?#{parameters}")
33
+ if response.class == Net::HTTPOK
34
+ return response
35
+ else
36
+ raise "An HTTP error occured."
37
+ end
38
+ }
39
+ end
40
+ end
41
+
42
+ class RandomOrg < OnlineGenerator
43
+ def initialize
44
+ super("www.random.org")
45
+ end
46
+
47
+ def randnum(num = 100, min = 1, max = 100)
48
+ if num < 0 || num > 10_000
49
+ raise RangeError, "Invalid amount: #{num}."
50
+ end
51
+ return [] if num == 0
52
+ if min < -1_000_000_000
53
+ raise RangeError, "Invalid minimum: #{min}."
54
+ end
55
+ if max > 1_000_000_000
56
+ raise RangeError, "Invalid maximum: #{max}."
57
+ end
58
+ if max <= min
59
+ raise RangeError, "Maximum has to be bigger than minimum."
60
+ end
61
+
62
+ parameters = "num=#{num}&min=#{min}&max=#{max}&col=#{num}"
63
+ response = get_response("/cgi-bin/randnum", parameters)
64
+ convert_result(response.body)
65
+ end
66
+
67
+ def randbyte(nbytes = 256)
68
+ if nbytes < 0 || nbytes > 16_384
69
+ raise RangeError, "Invalid amount: #{nbytes}."
70
+ end
71
+ return [] if nbytes == 0
72
+ parameters = "nbytes=#{nbytes}&format=d"
73
+ response = get_response("/cgi-bin/randbyte", parameters)
74
+ convert_result(response.body)
75
+ end
76
+
77
+ private
78
+
79
+ def convert_result(response)
80
+ result = []
81
+ response.each { |line|
82
+ result += line.chomp.split.map { |x| x.to_i }
83
+ }
84
+ result
85
+ end
86
+ end
87
+
88
+ class FourmiLab < OnlineGenerator
89
+ def initialize
90
+ super("www.fourmilab.ch")
91
+ end
92
+
93
+ def randbyte(nbytes = 128)
94
+ if nbytes < 0 || nbytes > 2048
95
+ raise RangeError, "Invalid amount: #{nbytes}."
96
+ end
97
+ return [] if nbytes == 0
98
+ parameters = "nbytes=#{nbytes}&fmt=bin"
99
+ response = get_response("/cgi-bin/uncgi/Hotbits", parameters)
100
+ if response['content-type'] != 'application/octet-stream'
101
+ raise "Unexpected content type: #{response['content-type']}."
102
+ end
103
+ result = []
104
+ response.body.each_byte { |b| result << b }
105
+ result
106
+ end
107
+ end
108
+
109
+ class EntropyPool < OnlineGenerator
110
+ def initialize
111
+ super("random.hd.org")
112
+ end
113
+
114
+ def randbyte(nbytes = 16, limit = true)
115
+ if nbytes < 0 || nbytes > 256
116
+ raise RangeError, "Invalid amount: #{nbytes}."
117
+ end
118
+ return [] if nbytes == 0
119
+ parameters = "numBytes=#{nbytes}&type=bin&limit=#{limit}"
120
+ response = get_response("/getBits.jsp", parameters)
121
+ if response['content-type'] !~ /application\/octet-stream/
122
+ raise "Unexpected content type: <#{response['content-type']}>."
123
+ end
124
+ result = []
125
+ response.body.each_byte { |b| result << b }
126
+ result
127
+ end
128
+ end
129
+ end
130
+
131
+ # vim:sw=2
132
+
File without changes
@@ -0,0 +1,34 @@
1
+ require 'date'
2
+ Gem::Specification.new do |s|
3
+ s.name = %q{realrand}
4
+ s.version = "1.0.2"
5
+ s.date = Date.today.to_s
6
+ s.summary = %q{Generate real random numbers with Ruby.}
7
+ s.description =<<DESCRIPTION
8
+ A lot of algorithms in cryptography etc. depend on good random numbers,
9
+ i.e. random numbers that are "real" random and not just generated by
10
+ a so called pseudo-random generator.
11
+
12
+ You cannot create real random numbers using a computer and an
13
+ algorithm. Only nature creates real randomness (just take a look around
14
+ the next time you are surrounded by a group of people.).
15
+
16
+ Real randomness occurs e.g. in atmospheric noise, during radioactive
17
+ decay, or in a lava lamp. Fortunately, you do not have to listen to an
18
+ old radio the whole day or, even worse, deposit some uranium in your
19
+ living room and observe it with a Geiger-M�ller tube. Other people do so
20
+ (in a slightly modified manner, of course) and they kindly make their
21
+ results public.
22
+ DESCRIPTION
23
+ s.author = %q{Maik Schmidt}
24
+ s.email = %q{contact@maik-schmidt.de}
25
+ s.homepage = %q{http://www.maik-schmidt.de/realrand.html}
26
+ s.files = Dir.glob('**/*')
27
+ s.require_paths = %w{. lib}
28
+ s.autorequire = %q{random/online}
29
+ s.has_rdoc = true
30
+ s.rdoc_options = ["--main", "README"]
31
+ s.extra_rdoc_files = ["README"]
32
+ s.test_files = %w{test/tc_all.rb}
33
+ s.required_ruby_version = Gem::Version::Requirement.new(">= 1.8.0")
34
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'tc_random_org'
5
+ require 'tc_fourmilab'
6
+ require 'tc_entropy_pool'
7
+
8
+ # vim:sw=2
9
+
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require '../lib/random/online'
5
+
6
+ class TC_EntropyPool < Test::Unit::TestCase # :nodoc:
7
+ def setup
8
+ @generator = Random::EntropyPool.new
9
+ end
10
+
11
+ def test_illegal_amount
12
+ [257, -1].each { |x|
13
+ assert_raises(RangeError) { @generator.randbyte(x) }
14
+ }
15
+ end
16
+
17
+ def test_zero_amount
18
+ assert_equal([], @generator.randbyte(0))
19
+ end
20
+
21
+ def test_randbyte
22
+ [1, 2, 10, 256].each { |x|
23
+ numbers = @generator.randbyte(x, false)
24
+ assert_equal(x, numbers.length)
25
+ }
26
+ end
27
+ end
28
+
29
+ # vim:sw=2
30
+
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require '../lib/random/online'
5
+
6
+ class TC_FourmiLab < Test::Unit::TestCase # :nodoc:
7
+ def setup
8
+ @generator = Random::FourmiLab.new
9
+ end
10
+
11
+ def test_illegal_amount
12
+ [2049, -1].each { |x|
13
+ assert_raises(RangeError) { @generator.randbyte(x) }
14
+ }
15
+ end
16
+
17
+ def test_zero_amount
18
+ assert_equal([], @generator.randbyte(0))
19
+ end
20
+
21
+ def test_randbyte
22
+ [1, 2, 10, 2048].each { |x|
23
+ numbers = @generator.randbyte(x)
24
+ assert_equal(x, numbers.length)
25
+ }
26
+ end
27
+ end
28
+
29
+ # vim:sw=2
30
+
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require '../lib/random/online'
5
+
6
+ class TC_RandomOrg < Test::Unit::TestCase # :nodoc:
7
+ def setup
8
+ @generator = Random::RandomOrg.new
9
+ end
10
+
11
+ def test_illegal_amount
12
+ [10_001, -1].each { |x|
13
+ assert_raises(RangeError) { @generator.randnum(x, 1, 100) }
14
+ }
15
+
16
+ [16_385, -1].each { |x|
17
+ assert_raises(RangeError) { @generator.randbyte(x) }
18
+ }
19
+
20
+ [
21
+ [1, -1_000_000_001, 100],
22
+ [1, 1, 1_000_000_001],
23
+ [1, 1, 0],
24
+ [1, 1, 1],
25
+ ].each { |num, min, max|
26
+ assert_raises(RangeError) { @generator.randnum(num, min, max) }
27
+ }
28
+ end
29
+
30
+ def test_zero_amount
31
+ assert_equal([], @generator.randnum(0, 1, 100))
32
+ assert_equal([], @generator.randbyte(0))
33
+ end
34
+
35
+ def test_randnum
36
+ [
37
+ [1, -1_000_000_000, 1_000_000_000],
38
+ [1, 1, 2],
39
+ [5, 20, 100],
40
+ [1000, 1, 6],
41
+ ].each { |num, min, max|
42
+ numbers = @generator.randnum(num, min, max)
43
+ assert_equal(num, numbers.length)
44
+ numbers.each { |x| assert(x >= min && x <= max) }
45
+ }
46
+ end
47
+
48
+ def test_randbyte
49
+ [1, 2, 10, 100].each { |x|
50
+ numbers = @generator.randbyte(x)
51
+ assert_equal(x, numbers.length)
52
+ }
53
+ end
54
+ end
55
+
56
+ # vim:sw=2
57
+
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: "0.8"
3
+ specification_version: 1
4
+ name: realrand
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.2
7
+ date: 2004-11-19
8
+ summary: Generate real random numbers with Ruby.
9
+ require_paths:
10
+ - "."
11
+ - lib
12
+ author: Maik Schmidt
13
+ email: contact@maik-schmidt.de
14
+ homepage: http://www.maik-schmidt.de/realrand.html
15
+ rubyforge_project:
16
+ description: "A lot of algorithms in cryptography etc. depend on good random numbers, i.e. random numbers that are \"real\" random and not just generated by a so called pseudo-random generator. You cannot create real random numbers using a computer and an algorithm. Only nature creates real randomness (just take a look around the next time you are surrounded by a group of people.). Real randomness occurs e.g. in atmospheric noise, during radioactive decay, or in a lava lamp. Fortunately, you do not have to listen to an old radio the whole day or, even worse, deposit some uranium in your living room and observe it with a Geiger-M�ller tube. Other people do so (in a slightly modified manner, of course) and they kindly make their results public."
17
+ autorequire: random/online
18
+ default_executable:
19
+ bindir: bin
20
+ has_rdoc: true
21
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
22
+ requirements:
23
+ -
24
+ - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.0
27
+ version:
28
+ platform: ruby
29
+ files:
30
+ - doc
31
+ - lib
32
+ - test
33
+ - LICENSE
34
+ - README
35
+ - install.rb
36
+ - INSTALL
37
+ - realrand-1.0.2.gem
38
+ - realrand.gemspec
39
+ - doc/readme.html
40
+ - lib/random
41
+ - lib/random/online.rb
42
+ - test/tc_entropy_pool.rb
43
+ - test/tc_fourmilab.rb
44
+ - test/tc_all.rb
45
+ - test/tc_random_org.rb
46
+ test_files:
47
+ - test/tc_all.rb
48
+ rdoc_options:
49
+ - "--main"
50
+ - README
51
+ extra_rdoc_files:
52
+ - README
53
+ executables: []
54
+ extensions: []
55
+ requirements: []
56
+ dependencies: []