LoginSignup
50
40

More than 5 years have passed since last update.

RubyでN桁のランダムな数字からなる文字列を得る

Last updated at Posted at 2013-07-10

SecureRandomにはそういうメソッドはないみたい。

こんな感じ?

require 'securerandom'

N = 10
p N.times.map { SecureRandom.random_number(10) }.join # "7260150113"

追記:
コメントより、以下のほうがずっと高速にできるようです。

p format("%0#{n}d", SecureRandom.random_number(10**n))
50
40
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
50
40