<p>Additionally, how can I format it as a string padded with zeros?</p>

[{'answer_id': 34582, 'author': 'quackingduck', 'author_id': 3624, 'author_profile': 'https://Stackoverflow.com/users/3624', 'pm_score': 7, 'selected': False, 'text': '<p>To generate the number call rand with the result of the expression "10 to the power of 10"</p>\n\n<pre><code>rand(10 ** 10)\n</code></pre>\n\n<p>To pad the number with zeros you can use the string format operator</p>\n\n<pre><code>\'%010d\' % rand(10 ** 10)\n</code></pre>\n\n<p>or the <code>rjust</code> method of string</p>\n\n<pre><code>rand(10 ** 10).to_s.rjust(10,\'0\')  \n</code></pre>\n'}, {'answer_id': 175678, 'author': 'Grant Hutchins', 'author_id': 6304, 'author_profile': 'https://Stackoverflow.com/users/6304', 'pm_score': 2, 'selected': False, 'text': "<p>Here is an expression that will use one fewer method call than quackingduck's example.</p>\n\n<pre><code>'%011d' % rand(1e10)\n</code></pre>\n\n<p>One caveat, <code>1e10</code> is a <code>Float</code>, and <code>Kernel#rand</code> ends up calling <code>to_i</code> on it, so for some higher values you might have some inconsistencies. To be more precise with a literal, you could also do:</p>\n\n<pre><code>'%011d' % rand(10_000_000_000) # Note that underscores are ignored in integer literals\n</code></pre>\n"}, {'answer_id': 11442975, 'author': 'Poseidon_Geek', 'author_id': 1269764, 'author_profile': 'https://Stackoverflow.com/users/1269764', 'pm_score': 2, 'selected': False, 'text': '<p>I just want to modify first answer. <code>rand (10**10)</code> may generate 9 digit random no if 0 is in first place. For ensuring 10 exact digit just modify </p>\n\n<pre><code>code = rand(10**10)\nwhile code.to_s.length != 10\ncode = rand(11**11)\n</code></pre>\n\n<p>end</p>\n'}, {'answer_id': 14501221, 'author': 'Paulo Fidalgo', 'author_id': 1006863, 'author_profile': 'https://Stackoverflow.com/users/1006863', 'pm_score': 0, 'selected': False, 'text': '<p>This will work even on ruby 1.8.7:</p>\n\n<blockquote>\n  <p>rand(9999999999).to_s.center(10, rand(9).to_s).to_i</p>\n</blockquote>\n'}, {'answer_id': 18161300, 'author': 'Kreeki', 'author_id': 559316, 'author_profile': 'https://Stackoverflow.com/users/559316', 'pm_score': 6, 'selected': False, 'text': '<p>I would like to contribute probably a simplest solution I know, which is a quite a good trick. </p>\n\n<pre><code>rand.to_s[2..11] \n =&gt; "5950281724"\n</code></pre>\n'}, {'answer_id': 18789508, 'author': 'Gabriel Osorio', 'author_id': 2192331, 'author_profile': 'https://Stackoverflow.com/users/2192331', 'pm_score': 3, 'selected': False, 'text': '<p>Just because it wasn\'t mentioned, the <code>Kernel#sprintf</code> method (or it\'s alias <code>Kernel#format</code> in the <a href="https://github.com/bbatsov/powerpack">Powerpack Library</a>) is generally preferred over the <code>String#%</code> method, as mentioned in the <a href="https://github.com/bbatsov/ruby-style-guide">Ruby Community Style Guide</a>.</p>\n\n<p>Of course this is highly debatable, but to provide insight:</p>\n\n<p>The syntax of @quackingduck\'s answer would be</p>\n\n<pre><code># considered bad\n\'%010d\' % rand(10**10)\n\n# considered good\nsprintf(\'%010d\', rand(10**10))\n</code></pre>\n\n<p>The nature of this preference is primarily due to the cryptic nature of <code>%</code>. It\'s not very semantic by itself and without any additional context it can be confused with the <code>%</code> modulo operator.</p>\n\n<p>Examples from the <a href="https://github.com/bbatsov/ruby-style-guide">Style Guide</a>:</p>\n\n<pre><code># bad\n\'%d %d\' % [20, 10]\n# =&gt; \'20 10\'\n\n# good\nsprintf(\'%d %d\', 20, 10)\n# =&gt; \'20 10\'\n\n# good\nsprintf(\'%{first} %{second}\', first: 20, second: 10)\n# =&gt; \'20 10\'\n\nformat(\'%d %d\', 20, 10)\n# =&gt; \'20 10\'\n\n# good\nformat(\'%{first} %{second}\', first: 20, second: 10)\n# =&gt; \'20 10\'\n</code></pre>\n\n<p>To make justice for <code>String#%</code>, I personally really like using operator-like syntaxes instead of commands, the same way you would do <code>your_array &lt;&lt; \'foo\'</code> over <code>your_array.push(\'123\')</code>.</p>\n\n<p>This just illustrates a tendency in the community, what\'s "best" is up to you.</p>\n\n<p>More info in <a href="http://batsov.com/articles/2013/06/27/the-elements-of-style-in-ruby-number-2-favor-sprintf-format-over-string-number-percent/">this blogpost</a>.</p>\n'}, {'answer_id': 25682945, 'author': 'Tan Nguyen', 'author_id': 2245697, 'author_profile': 'https://Stackoverflow.com/users/2245697', 'pm_score': 1, 'selected': False, 'text': '<pre><code>rand(9999999999).to_s.center(10, rand(9).to_s).to_i\n</code></pre>\n\n<p>is faster than</p>\n\n<pre><code>rand.to_s[2..11].to_i\n</code></pre>\n\n<p>You can use:</p>\n\n<pre><code>puts Benchmark.measure{(1..1000000).map{rand(9999999999).to_s.center(10, rand(9).to_s).to_i}}\n</code></pre>\n\n<p>and</p>\n\n<pre><code>puts Benchmark.measure{(1..1000000).map{rand.to_s[2..11].to_i}}\n</code></pre>\n\n<p>in Rails console to confirm that.</p>\n'}, {'answer_id': 30456635, 'author': 'Kamil Lelonek', 'author_id': 1313175, 'author_profile': 'https://Stackoverflow.com/users/1313175', 'pm_score': 4, 'selected': False, 'text': '<h2>DON\'T USE <code>rand.to_s[2..11].to_i</code></h2>\n\n<p>Why? Because here\'s what you can get:</p>\n\n<pre><code>rand.to_s[2..9] #=&gt; "04890612"\n</code></pre>\n\n<p>and then:</p>\n\n<pre><code>"04890612".to_i #=&gt; 4890612\n</code></pre>\n\n<p>Note that:</p>\n\n<pre><code>4890612.to_s.length #=&gt; 7\n</code></pre>\n\n<p>Which is not what you\'ve expected!</p>\n\n<p>To check that error in your own code, instead of <code>.to_i</code> you may wrap it like this:</p>\n\n<pre><code>Integer(rand.to_s[2..9])\n</code></pre>\n\n<p>and very soon it will turn out that:</p>\n\n<pre><code>ArgumentError: invalid value for Integer(): "02939053"\n</code></pre>\n\n<p>So it\'s always better to stick to <code>.center</code>, but keep in mind that:</p>\n\n<pre><code>rand(9) \n</code></pre>\n\n<p>sometimes may give you <code>0</code>.</p>\n\n<p>To prevent that:</p>\n\n<pre><code>rand(1..9)\n</code></pre>\n\n<p>which will always return something withing <code>1..9</code> range.</p>\n\n<p>I\'m glad that I had good tests and I hope you will avoid breaking your system.</p>\n'}, {'answer_id': 31021140, 'author': 'Dharani Manne', 'author_id': 2893591, 'author_profile': 'https://Stackoverflow.com/users/2893591', 'pm_score': 2, 'selected': False, 'text': '<p>Try using the SecureRandom ruby library.</p>\n\n<p>It generates random numbers but the length is not specific.</p>\n\n<p>Go through this link for more information: <a href="http://ruby-doc.org/stdlib-2.1.2/libdoc/securerandom/rdoc/SecureRandom.html" rel="nofollow">http://ruby-doc.org/stdlib-2.1.2/libdoc/securerandom/rdoc/SecureRandom.html</a></p>\n'}, {'answer_id': 31043708, 'author': 'John La Rooy', 'author_id': 174728, 'author_profile': 'https://Stackoverflow.com/users/174728', 'pm_score': 2, 'selected': False, 'text': '<p>This technique works for any "alphabet"</p>\n\n<pre><code>(1..10).map{"0123456789".chars.to_a.sample}.join\n=&gt; "6383411680"\n</code></pre>\n'}, {'answer_id': 31043825, 'author': 'art-solopov', 'author_id': 2733119, 'author_profile': 'https://Stackoverflow.com/users/2733119', 'pm_score': 4, 'selected': False, 'text': '<p>The most straightforward answer would probably be</p>\n\n<p><code>rand(1e9...1e10).to_i</code></p>\n\n<p>The <code>to_i</code> part is needed because <code>1e9</code> and <code>1e10</code> are actually floats:</p>\n\n<pre><code>irb(main)&gt; 1e9.class\n=&gt; Float\n</code></pre>\n'}, {'answer_id': 35785079, 'author': 'steenslag', 'author_id': 290394, 'author_profile': 'https://Stackoverflow.com/users/290394', 'pm_score': 5, 'selected': False, 'text': '<p>This is a fast way to generate a 10-sized string of digits:</p>\n\n<pre><code>10.times.map{rand(10)}.join # =&gt; "3401487670"\n</code></pre>\n'}, {'answer_id': 39995528, 'author': 'Rahul Patel', 'author_id': 2334864, 'author_profile': 'https://Stackoverflow.com/users/2334864', 'pm_score': 2, 'selected': False, 'text': '<p>Simplest way to generate n digit random number - </p>\n\n<pre><code>Random.new.rand((10**(n - 1))..(10**n))\n</code></pre>\n\n<p>generate 10 digit number number -</p>\n\n<pre><code>Random.new.rand((10**(10 - 1))..(10**10))\n</code></pre>\n'}, {'answer_id': 43346807, 'author': '907th', 'author_id': 1247092, 'author_profile': 'https://Stackoverflow.com/users/1247092', 'pm_score': 4, 'selected': False, 'text': '<h2>Random number generation</h2>\n\n<p>Use <a href="https://ruby-doc.org/core-2.2.3/Kernel.html#method-i-rand" rel="noreferrer">Kernel#rand</a> method:</p>\n\n<pre><code>rand(1_000_000_000..9_999_999_999) # =&gt; random 10-digits number\n</code></pre>\n\n<h2>Random string generation</h2>\n\n<p>Use <code>times</code> + <code>map</code> + <code>join</code> combination:</p>\n\n<pre><code>10.times.map { rand(0..9) }.join # =&gt; random 10-digit string (may start with 0!)\n</code></pre>\n\n<h2>Number to string conversion with padding</h2>\n\n<p>Use <a href="https://ruby-doc.org/core-2.2.0/String.html#method-i-25" rel="noreferrer">String#%</a> method:</p>\n\n<pre><code>"%010d" % 123348 # =&gt; "0000123348"\n</code></pre>\n\n<h2>Password generation</h2>\n\n<p>Use <a href="https://github.com/johnbintz/keepass-password-generator" rel="noreferrer">KeePass password generator</a> library, it supports different patterns for generating random password:</p>\n\n<pre><code>KeePass::Password.generate("d{10}") # =&gt; random 10-digit string (may start with 0!)\n</code></pre>\n\n<p>A documentation for KeePass patterns can be found <a href="http://keepass.info/help/base/pwgenerator.html" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 45458745, 'author': 'Tom Lord', 'author_id': 1954610, 'author_profile': 'https://Stackoverflow.com/users/1954610', 'pm_score': 1, 'selected': False, 'text': '<p>An alternative answer, using the <a href="https://github.com/tom-lord/regexp-examples" rel="nofollow noreferrer"><code>regexp-examples</code></a> ruby gem:</p>\n\n<pre><code>require \'regexp-examples\'\n\n/\\d{10}/.random_example # =&gt; "0826423747"\n</code></pre>\n\n<p>There\'s no need to "pad with zeros" with this approach, since you are immediately generating a <code>String</code>.</p>\n'}, {'answer_id': 50966721, 'author': 'Anderson Marques', 'author_id': 6646873, 'author_profile': 'https://Stackoverflow.com/users/6646873', 'pm_score': 0, 'selected': False, 'text': '<p>A better approach is use <code>Array.new()</code> instead of <code>.times.map</code>. Rubocop recommends it.</p>\n\n<p>Example:</p>\n\n<pre><code>string_size = 9\nArray.new(string_size) do\n   rand(10).to_s\nend\n</code></pre>\n\n<p>Rubucop, TimesMap:</p>\n\n<p><a href="https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Performance/TimesMap" rel="nofollow noreferrer">https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Performance/TimesMap</a></p>\n'}, {'answer_id': 52075422, 'author': 'Lalit Kumar Maurya', 'author_id': 1637683, 'author_profile': 'https://Stackoverflow.com/users/1637683', 'pm_score': 2, 'selected': False, 'text': '<p>Just use straightforward below.</p>\n\n<pre><code>rand(10 ** 9...10 ** 10)\n</code></pre>\n\n<p>Just test it on IRB with below.</p>\n\n<pre><code>(1..1000).each { puts rand(10 ** 9...10 ** 10) }\n</code></pre>\n'}, {'answer_id': 53654592, 'author': 'Mario Ruiz', 'author_id': 2546667, 'author_profile': 'https://Stackoverflow.com/users/2546667', 'pm_score': -1, 'selected': False, 'text': '<p>Random 10 numbers:</p>\n\n<pre><code>require \'string_pattern\'\nputs "10:N".gen\n</code></pre>\n'}, {'answer_id': 54283486, 'author': 'kazuwombat', 'author_id': 5992952, 'author_profile': 'https://Stackoverflow.com/users/5992952', 'pm_score': 0, 'selected': False, 'text': '<p>In my case number must be unique in my models, so I added checking block.</p>\n\n<pre><code>  module StringUtil\n    refine String.singleton_class do\n      def generate_random_digits(size:)\n        proc = lambda{ rand.to_s[2...(2 + size)] }\n        if block_given?\n          loop do\n            generated = proc.call\n            break generated if yield(generated) # check generated num meets condition\n          end\n        else\n          proc.call\n        end\n      end\n    end\n  end\n  using StringUtil\n  String.generate_random_digits(3) =&gt; "763"\n  String.generate_random_digits(3) do |num|\n    User.find_by(code: num).nil?\n  end =&gt; "689"(This is unique in Users code)\n</code></pre>\n'}, {'answer_id': 55379711, 'author': 'Khalil Gharbaoui', 'author_id': 5641227, 'author_profile': 'https://Stackoverflow.com/users/5641227', 'pm_score': 2, 'selected': False, 'text': '<p><code>(\'%010d\' % rand(0..9999999999)).to_s</code></p>\n\n<p>or</p>\n\n<p><code>"#{\'%010d\' % rand(0..9999999999)}"</code></p>\n'}, {'answer_id': 56564214, 'author': 'Eliot Sykes', 'author_id': 67834, 'author_profile': 'https://Stackoverflow.com/users/67834', 'pm_score': 2, 'selected': False, 'text': '<p>To generate a random, 10-digit string:</p>\n\n<pre><code># This generates a 10-digit string, where the\n# minimum possible value is "0000000000", and the\n# maximum possible value is "9999999999"\nSecureRandom.random_number(10**10).to_s.rjust(10, \'0\')\n</code></pre>\n\n<hr>\n\n<p>Here\'s more detail of what\'s happening, shown by breaking the single line into multiple lines with explaining variables:</p>\n\n<pre><code>  # Calculate the upper bound for the random number generator\n  # upper_bound = 10,000,000,000\n  upper_bound = 10**10\n\n  # n will be an integer with a minimum possible value of 0,\n  # and a maximum possible value of 9,999,999,999\n  n = SecureRandom.random_number(upper_bound)\n\n  # Convert the integer n to a string\n  # unpadded_str will be "0" if n == 0\n  # unpadded_str will be "9999999999" if n == 9_999_999_999\n  unpadded_str = n.to_s\n\n  # Pad the string with leading zeroes if it is less than\n  # 10 digits long.\n  # "0" would be padded to "0000000000"\n  # "123" would be padded to "0000000123"\n  # "9999999999" would not be padded, and remains unchanged as "9999999999"\n  padded_str = unpadded_str.rjust(10, \'0\')\n</code></pre>\n'}, {'answer_id': 63454165, 'author': 'Anton Semenichenko', 'author_id': 7809709, 'author_profile': 'https://Stackoverflow.com/users/7809709', 'pm_score': 3, 'selected': False, 'text': '<p>I ended up with using Ruby kernel <code>srand</code></p>\n<p><code>srand.to_s.last(10)</code></p>\n<p>Docs here: <a href="https://www.rubydoc.info/stdlib/core/Kernel:srand" rel="noreferrer">Kernel#srand</a></p>\n'}, {'answer_id': 67246974, 'author': 'MD Tawab Alam Khan', 'author_id': 11111981, 'author_profile': 'https://Stackoverflow.com/users/11111981', 'pm_score': 0, 'selected': False, 'text': '<p>I did something like this</p>\n<pre><code>x = 10  #Number of digit\n(rand(10 ** x) + 10**x).to_s[0..x-1]\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/34565', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3624/']