Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
35
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@youcune

Rangeの範囲内からランダムに数値を選ぶ

誤りの例

以下で一見良さそうなんですが、そもそもやりたいことができません。Rangeオブジェクトいっこの配列ができるので、この返り値はRangeになります。

[1 .. 10000].sample

好ましくない例

これもダメです。to_aしたらメモリに展開され、数が少なければそんなに影響ないですが、多いと悲劇になります。

(1 .. 10000).to_a.sample

よい例

Random#randは引数にRangeを渡せます。

Random.rand(1 .. 1000)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
35
Help us understand the problem. What is going on with this article?