pixiedust 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,7 +1,17 @@
1
+ == 0.0.5 / 2008-05-14
2
+ * Sped up RandomDust, no longer raising errors and catching, 2x faster
3
+ * Added
4
+ * DateDust.now_stru() - Date string like 200805141711 (with usec)
5
+ * Object.try() - from Chris Wanstrath http://ozmm.org/posts/try.html
6
+
7
+ == 0.0.4 / 2008-02-28
8
+ * Restructured for Hoe
9
+
1
10
  == 0.0.3 / 2008-02-27
2
11
 
3
12
  * Initial release
4
- * RandomDust.between
5
- * NumberDust.nearest
6
- * DateDust.now_str
13
+ * RandomDust.between(min, max) - Unskewed number between min and max
14
+ * NumberDust.nearest() - Number nearest order of magnitude
15
+ (9==9, 10000002351 = 10000000000)
16
+ * DateDust.now_str() - Date string like 200805141711
7
17
 
data/README.txt CHANGED
@@ -4,16 +4,21 @@ PixieDust
4
4
 
5
5
  == DESCRIPTION:
6
6
  A collection of little utility functions (many security related) for assorted
7
- random tasks in Ruby
7
+ tasks in Ruby
8
8
 
9
9
  Where applicable, they come with a standard version and a class mix in.
10
10
  Ex:
11
11
  NumberDust.nearest(value) is also available as Integer.pd_nearest
12
12
 
13
13
  == FEATURES/PROBLEMS:
14
- * Get random numbers in arbitrary ranges from OpenSSL (without skew due
14
+ Features:
15
+ * Get random numbers in arbitrary ranges from OpenSSL (without skew due
15
16
  to modulo)
16
17
 
18
+ Problems:
19
+ * Need a good way to make sure that the OpenSSL library isn't using a
20
+ broken PRNG
21
+
17
22
  == SYNOPSIS:
18
23
  require 'pixiedust'
19
24
  x = RandomDust.between(10,20)
data/lib/pixiedust.rb CHANGED
@@ -5,10 +5,16 @@ rescue LoadError
5
5
  end
6
6
 
7
7
  class PixieDust
8
- VERSION = '0.0.4'
8
+ VERSION = '0.0.5'
9
9
  end
10
10
 
11
- class PDRandOutOfRange < RangeError; end
11
+ class Object
12
+ # from Chris Wanstrath http://ozmm.org/posts/try.html
13
+ # @person ? @person.name : nil vs @person.try(:name)
14
+ def try(method)
15
+ send method if respond_to? method
16
+ end
17
+ end
12
18
 
13
19
  class RandomDust
14
20
  # XXX: Do something to make sure that OpenSSL is using a real RNG
@@ -32,7 +38,7 @@ class RandomDust
32
38
  else unp=""
33
39
  end
34
40
 
35
- begin
41
+ while true
36
42
  rand = OpenSSL::Random.random_bytes(bytes_used)
37
43
  parts = rand.unpack(unp + "L"*(uints))
38
44
  value = parts.shift
@@ -42,15 +48,19 @@ class RandomDust
42
48
 
43
49
  value += min
44
50
 
45
- if value < min || value > max then raise PDRandOutOfRange; end
46
- rescue PDRandOutOfRange
47
- retry
51
+ unless value < min || value > max
52
+ return value
53
+ end
48
54
  end
49
- value
50
55
  end
51
56
  end
52
57
 
53
58
  class DateDust
59
+ def DateDust.now_stru
60
+ str = sprintf("%d%02d%02d%02d%02d%02d%06d", Time.now.year, Time.now.month,
61
+ Time.now.day, Time.now.hour, Time.now.min, Time.now.sec, Time.now.usec)
62
+ end
63
+
54
64
  def DateDust.now_str
55
65
  str = sprintf("%d%02d%02d%02d%02d%02d", Time.now.year, Time.now.month,
56
66
  Time.now.day, Time.now.hour, Time.now.min, Time.now.sec)
data/test/random_dust.rb CHANGED
@@ -1,17 +1,29 @@
1
1
  require 'rubygems'
2
2
  require 'spec'
3
+ require 'statarray'
4
+ begin
5
+ require 'ruby-prof'
6
+ profile = false
7
+ rescue LoadError
8
+ end
3
9
 
4
- require "#{File.dirname(__FILE__)}/../pixiedust"
10
+ require "#{File.dirname(__FILE__)}/../lib/pixiedust"
5
11
 
6
12
  describe RandomDust do
7
13
  it "Shouldn't generate numbers outside of range" do
8
14
  min = -5
9
15
  max = 15
10
- 100.times {
16
+ if profile then RubyProf.start; end
17
+ 1000.times {
11
18
  val = RandomDust.between(min,max)
12
19
  val.should <= max
13
20
  val.should >= min
14
21
  }
22
+ if profile
23
+ profile_data = RubyProf.stop
24
+ printer = RubyProf::FlatPrinter.new(profile_data)
25
+ printer.print(open("profile-#{DateDust.now_str}.txt", "w+"), 0)
26
+ end
15
27
  end
16
28
 
17
29
  it "Should generate numbers at ends of range" do
@@ -26,4 +38,30 @@ describe RandomDust do
26
38
  hit_min.should == true
27
39
  hit_max.should == true
28
40
  end
41
+
42
+ it "Should have roughly equal counts of numbers in range" do
43
+ min = 4
44
+ max = 55
45
+ values = {}
46
+ vlist = []
47
+ 100000.times {
48
+ val = RandomDust.between(min, max)
49
+ values[val] ||= 0
50
+ values[val] += 1
51
+ vlist << val
52
+ }
53
+
54
+ counts = []
55
+ values.keys.each { |k|
56
+ counts << values[k]
57
+ }
58
+
59
+ ca = counts.to_statarray
60
+ #puts "min: #{ca.min}"
61
+ #puts "max: #{ca.max}"
62
+ #puts "median: #{ca.median}"
63
+ #puts "stddev: #{ca.stddev}"
64
+ ca.stddev.should < 100
65
+
66
+ end
29
67
  end
metadata CHANGED
@@ -1,33 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: pixiedust
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.0.4
7
- date: 2008-02-28 00:00:00 -08:00
8
- summary: A collection of little routines and functions
9
- require_paths:
10
- - lib
11
- email: jwilkins[at]bitland[dot]net
12
- homepage: " by Jonathan Wilkins"
13
- rubyforge_project: pixiedust
14
- description: "Where applicable, they come with a standard version and a class mix in. Ex: NumberDust.nearest(value) is also available as Integer.pd_nearest == FEATURES/PROBLEMS: * Get random numbers in arbitrary ranges from OpenSSL (without skew due to modulo) == SYNOPSIS: require 'pixiedust' x = RandomDust.between(10,20) == REQUIREMENTS: None yet."
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.0.5
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - Jonathan Wilkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.1
23
+ version:
24
+ description: "Where applicable, they come with a standard version and a class mix in. Ex: NumberDust.nearest(value) is also available as Integer.pd_nearest == FEATURES/PROBLEMS: Features: * Get random numbers in arbitrary ranges from OpenSSL (without skew due to modulo) Problems: * Need a good way to make sure that the OpenSSL library isn't using a broken PRNG == SYNOPSIS: require 'pixiedust' x = RandomDust.between(10,20)"
25
+ email: jwilkins[at]bitland[dot]net
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - History.txt
32
+ - Manifest.txt
33
+ - README.txt
31
34
  files:
32
35
  - History.txt
33
36
  - Manifest.txt
@@ -36,28 +39,32 @@ files:
36
39
  - lib/pixiedust.rb
37
40
  - test/number_dust.rb
38
41
  - test/random_dust.rb
39
- test_files: []
40
-
42
+ has_rdoc: true
43
+ homepage: " by Jonathan Wilkins"
44
+ post_install_message:
41
45
  rdoc_options:
42
46
  - --main
43
47
  - README.txt
44
- extra_rdoc_files:
45
- - History.txt
46
- - Manifest.txt
47
- - README.txt
48
- executables: []
49
-
50
- extensions: []
51
-
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
52
62
  requirements: []
53
63
 
54
- dependencies:
55
- - !ruby/object:Gem::Dependency
56
- name: hoe
57
- version_requirement:
58
- version_requirements: !ruby/object:Gem::Version::Requirement
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: 1.4.0
63
- version:
64
+ rubyforge_project: pixiedust
65
+ rubygems_version: 1.0.1
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: A collection of little routines and functions
69
+ test_files: []
70
+