class Dummer::Random
Public Class Methods
new()
click to toggle source
# 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
Public Instance Methods
any(any)
click to toggle source
# File lib/dummer/random.rb, line 99 def any(any) any[rand(any.size)] end
datetime(opts = {})
click to toggle source
# 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 = {})
click to toggle source
# 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 = {})
click to toggle source
# 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)
click to toggle source
# File lib/dummer/random.rb, line 103 def rand(arg = nil) @rand.rand(arg) end
range(range)
click to toggle source
private
# File lib/dummer/random.rb, line 95 def range(range) rand(range) end
string(opts = {})
click to toggle source
belows are data types @return Proc object which returns a random generated value, or [formatted_value, raw_value]
# 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