Class: Dummer::Random
- Inherits:
-
Object
- Object
- Dummer::Random
- Defined in:
- lib/dummer/random.rb
Instance Method Summary collapse
- #any(any) ⇒ Object
- #datetime(opts = {}) ⇒ Object
- #float(opts = {}) ⇒ Object
-
#initialize ⇒ Random
constructor
A new instance of Random.
- #integer(opts = {}) ⇒ Object
- #rand(arg = nil) ⇒ Object
-
#range(range) ⇒ Object
private.
-
#string(opts = {}) ⇒ Object
belows are data types.
Constructor Details
#initialize ⇒ Random
Returns a new instance of Random.
3 4 5 6 |
# File 'lib/dummer/random.rb', line 3 def initialize @rand = ::Random.new(0) @chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a # no symbols and multi-bytes for now end |
Instance Method Details
#any(any) ⇒ Object
99 100 101 |
# File 'lib/dummer/random.rb', line 99 def any(any) any[rand(any.size)] end |
#datetime(opts = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dummer/random.rb', line 73 def datetime(opts = {}) format, random, value = (opts[:format] || "%Y-%m-%d %H:%M:%S.%3N"), (opts[:random] || false), opts[:value] if value Proc.new { value.strftime(format) } elsif random Proc.new { y = rand(1970..2037); m = rand(1..12); d = rand(1..27); h = rand(0..23); min = rand(0..59); s = rand(0..59); usec = rand(0..999999); Time.local(y, m, d, h, min, s, usec).strftime(format) } else Proc.new { Time.now.strftime(format) } end end |
#float(opts = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dummer/random.rb', line 50 def float(opts = {}) format, range, value = opts[:format], opts[:range], opts[:value] if format if value float = value.to_f Proc.new { sprintf(format, float) } elsif range Proc.new { sprintf(format, self.range(range)) } else Proc.new { r = rand(1..358); sprintf(format, r * Math.cos(r)) } end else if value float = value.to_f Proc.new { float } elsif range Proc.new { self.range(range) } else Proc.new { r = rand(1..358); r * Math.cos(r) } end end end |
#integer(opts = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dummer/random.rb', line 23 def integer(opts = {}) format, range, countup, value = opts[:format], opts[:range], opts[:countup], opts[:value] if format if value integer = sprintf(format, value.to_i) Proc.new { integer } elsif range Proc.new { sprintf(format, self.range(range)) } elsif countup Proc.new {|prev| v = prev + 1; [sprintf(format, v), v] } else Proc.new { sprintf(format, rand(0..2,147,483,647)) } end else if value integer = value.to_i Proc.new { integer } elsif range Proc.new { self.range(range) } elsif countup Proc.new {|prev| prev + 1 } else Proc.new { rand(0..2,147,483,647) } end end end |
#rand(arg = nil) ⇒ Object
103 104 105 |
# File 'lib/dummer/random.rb', line 103 def rand(arg = nil) @rand.rand(arg) end |
#range(range) ⇒ Object
private
95 96 97 |
# File 'lib/dummer/random.rb', line 95 def range(range) rand(range) end |
#string(opts = {}) ⇒ Object
belows are data types
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dummer/random.rb', line 11 def string(opts = {}) length, any, value = (opts[:length] || 8), opts[:any], opts[:value] if value string = value.to_s Proc.new { string } elsif any Proc.new { self.any(any) } else Proc.new { Array.new(length){@chars[rand(@chars.size-1)]}.join } end end |