Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Rubyを使用して40から47までの乱数を生成するにはどうすればよいですか? 私はそれを試しましたが、理解できませんでした。Rubyで特定の範囲の乱数を生成するにはどうすればよいですか?
rand(8)0 から 7 の間の乱数を生成します (その範囲には実際に 8 つの数値があることに注意してください)。
rand(8)
この範囲に 40 を追加するだけで、40 から 47 の間の数値が得られます。次のようにします。
40 + rand(8)
あなたはこれを試すことができますrand(40..47)
rand(40..47)