LoginSignup
32
30

More than 5 years have passed since last update.

1からNまでのランダムな数字を取得する

Last updated at Posted at 2013-11-20

タイトルの通り、実行する度に得られる数字が異なるようにしたかった。

軽く調べてみた感じ rand を使ってるサンプルが多かった。

1から10までの数字をランダムに返す。

sample_rand.rb
rand(10) + 1

このやり方よりも自分的には sample を使ったやり方の方が好きかなと。

sample_sample.rb
[*1..10].sample

ただし、1からNまでのサイズの配列を作るので大きな桁の
ランダムな値を取得するにはあまり適してないかも。

32
30
1

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
32
30