How do I generate a random 10 digit number in ruby?

Viewed 60443

Additionally, how can I format it as a string padded with zeros?

23 Answers

To generate the number call rand with the result of the expression "10 to the power of 10"

rand(10 ** 10)

To pad the number with zeros you can use the string format operator

'%010d' % rand(10 ** 10)

or the rjust method of string

rand(10 ** 10).to_s.rjust(10,'0')  

I ended up with using Ruby kernel srand

srand.to_s.last(10)

Docs here: Kernel#srand

Here is an expression that will use one fewer method call than quackingduck's example.

'%011d' % rand(1e10)

One caveat, 1e10 is a Float, and Kernel#rand ends up calling to_i on it, so for some higher values you might have some inconsistencies. To be more precise with a literal, you could also do:

'%011d' % rand(10_000_000_000) # Note that underscores are ignored in integer literals

('%010d' % rand(0..9999999999)).to_s

or

"#{'%010d' % rand(0..9999999999)}"

Simplest way to generate n digit random number -

Random.new.rand((10**(n - 1))..(10**n))

generate 10 digit number number -

Random.new.rand((10**(10 - 1))..(10**10))

Just use straightforward below.

rand(10 ** 9...10 ** 10)

Just test it on IRB with below.

(1..1000).each { puts rand(10 ** 9...10 ** 10) }

To generate a random, 10-digit string:

# This generates a 10-digit string, where the
# minimum possible value is "0000000000", and the
# maximum possible value is "9999999999"
SecureRandom.random_number(10**10).to_s.rjust(10, '0')

Here's more detail of what's happening, shown by breaking the single line into multiple lines with explaining variables:

  # Calculate the upper bound for the random number generator
  # upper_bound = 10,000,000,000
  upper_bound = 10**10

  # n will be an integer with a minimum possible value of 0,
  # and a maximum possible value of 9,999,999,999
  n = SecureRandom.random_number(upper_bound)

  # Convert the integer n to a string
  # unpadded_str will be "0" if n == 0
  # unpadded_str will be "9999999999" if n == 9_999_999_999
  unpadded_str = n.to_s

  # Pad the string with leading zeroes if it is less than
  # 10 digits long.
  # "0" would be padded to "0000000000"
  # "123" would be padded to "0000000123"
  # "9999999999" would not be padded, and remains unchanged as "9999999999"
  padded_str = unpadded_str.rjust(10, '0')

In my case number must be unique in my models, so I added checking block.

  module StringUtil
    refine String.singleton_class do
      def generate_random_digits(size:)
        proc = lambda{ rand.to_s[2...(2 + size)] }
        if block_given?
          loop do
            generated = proc.call
            break generated if yield(generated) # check generated num meets condition
          end
        else
          proc.call
        end
      end
    end
  end
  using StringUtil
  String.generate_random_digits(3) => "763"
  String.generate_random_digits(3) do |num|
    User.find_by(code: num).nil?
  end => "689"(This is unique in Users code)

I did something like this

x = 10  #Number of digit
(rand(10 ** x) + 10**x).to_s[0..x-1]

Random 10 numbers:

require 'string_pattern'
puts "10:N".gen
Related