Method: Wpxf::Utility::Text.rand_numeric

Defined in:
lib/wpxf/utility/text.rb

.rand_numeric(length, allow_leading_zero = false) ⇒ String

Generate a random numeric string.

Parameters:

  • length (Integer)

    the number of characters to include.

  • allow_leading_zero (Boolean) (defaults to: false)

    if set to true, will allow a number starting with zero.

Returns:

  • (String)

    a random numeric string.



13
14
15
16
17
# File 'lib/wpxf/utility/text.rb', line 13

def self.rand_numeric(length, allow_leading_zero = false)
  value = Array.new(length) { [*'0'..'9'].sample }.join
  value[0] = [*'1'..'9'].sample unless allow_leading_zero
  value
end