lib/rantly/generator.rb in rantly-1.1.0 vs lib/rantly/generator.rb in rantly-1.2.0

- old
+ new

@@ -40,11 +40,11 @@ def tries @nfailed end def to_s - "Exceed gen limit #{@limit}: #{@nfailed} failed guards)" + "Exceed gen limit #{@limit}: #{@nfailed} failed guards" end end # limit attempts to 10 times of how many things we want to generate def each(n,limit=10,&block) @@ -119,11 +119,11 @@ @size = old_size return r end # wanna avoid going into Bignum when calling range with these. - INTEGER_MAX = (2**(0.size * 8 -2) -1) / 2 + INTEGER_MAX = (2**(0.size * 8 - 2) - 1) / 2 INTEGER_MIN = -(INTEGER_MAX) def integer(limit=nil) case limit when Range hi = limit.end @@ -139,12 +139,22 @@ def positive_integer range(0) end - def float - rand + def float(distribution=nil, params={}) + case distribution + when :normal + params[:center] ||= 0 + params[:scale] ||= 1 + raise "The distribution scale should be greater than zero" unless params[:scale] > 0 + # Sum of 6 draws from a uniform distribution give as a draw of a normal + # distribution centered in 3 (central limit theorem). + ([rand, rand, rand, rand, rand, rand].reduce(0, :+) - 3) * params[:scale] + params[:center] + else + rand + end end def range(lo=nil,hi=nil) lo ||= INTEGER_MIN hi ||= INTEGER_MAX @@ -228,22 +238,22 @@ def of(regexp) ASCII.scan(regexp).to_a.map! { |char| char[0].ord } end end - ALNUM = Chars.of /[[:alnum:]]/ - ALPHA = Chars.of /[[:alpha:]]/ - BLANK = Chars.of /[[:blank:]]/ - CNTRL = Chars.of /[[:cntrl:]]/ - DIGIT = Chars.of /[[:digit:]]/ - GRAPH = Chars.of /[[:graph:]]/ - LOWER = Chars.of /[[:lower:]]/ - PRINT = Chars.of /[[:print:]]/ - PUNCT = Chars.of /[[:punct:]]/ - SPACE = Chars.of /[[:space:]]/ - UPPER = Chars.of /[[:upper:]]/ - XDIGIT = Chars.of /[[:xdigit:]]/ - ASCII = Chars.of /./ + ALNUM = Chars.of(/[[:alnum:]]/) + ALPHA = Chars.of(/[[:alpha:]]/) + BLANK = Chars.of(/[[:blank:]]/) + CNTRL = Chars.of(/[[:cntrl:]]/) + DIGIT = Chars.of(/[[:digit:]]/) + GRAPH = Chars.of(/[[:graph:]]/) + LOWER = Chars.of(/[[:lower:]]/) + PRINT = Chars.of(/[[:print:]]/) + PUNCT = Chars.of(/[[:punct:]]/) + SPACE = Chars.of(/[[:space:]]/) + UPPER = Chars.of(/[[:upper:]]/) + XDIGIT = Chars.of(/[[:xdigit:]]/) + ASCII = Chars.of(/./) CLASSES = { :alnum => ALNUM, :alpha => ALPHA,