Sha256: d7d8d4eb6973f97866cd46e55ad043ff9b465202a55b63bc2ebb0fd9d12b0257

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module RandomDataDespegar
  module Numbers
    #n can be an Integer or a Range.  If it is an Integer, it just returns a random
    #number greater than or equal to 0 and less than n.  If it is a Range, it
    #returns a random number within the range
    # Examples
    #
    # >> Random.number(5)
    # => 4
    # >> Random.number(5)
    # => 2
    # >> Random.number(5)
    # => 1
    def number(n)
      n.is_a?(Range) ? n.to_a.rand : rand(n)
    end

    # number_size is the size of the number to be generated.
    # >> random_number_with_size (5)
    # => "12345"
    # >> random_number_with_size(10)
    # => "1234321234"
    # >> random_number_with_size(20)
    # => "25895277473360037460"
    def number_with_size(number_size)
      Array.new(number_size){rand(9)}.join
    end

    # Returns an alphanumeric number with size n
    def alphanumeric_with_size(number_size)
      Random.alphanumeric(number_size)
    end

    # return a random bit, 0 or 1.
    def bit
      rand(2)
    end

    # return an array of n random bits.
    def bits(n)
      x = []
      n.times {x << bit}
      x
    end

    # Return card numbers, based on a regex and card number length.
    # regex will be a string
    # length will be an integer
    #
    def random_card_number (regex, length)
      regex = regex.chomp("$").chomp("+")+"{"+(length.to_s)+"}"
      /#{regex}/.random_example.slice(0,length)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
random_data_despegar-2.1 lib/random_data_despegar/numbers.rb