-1

How can generate a random number between 40 and 47 using ruby? I tried that but could not understand. How do I generate a random number in a certain range with Ruby?

Community
  • 1
  • 1
cola
  • 12,198
  • 36
  • 105
  • 165
  • This question is really vague. What exactly did you not understand, and equally important, what did you understand or do you think the given solutions do? – Holger Just Apr 23 '12 at 09:54

2 Answers2

4

rand(8) will generate a random number between 0 and 7 (note that there are indeed 8 numbers in that range).

All you want to do is add 40 to that range, so you'll get numbers between 40 and 47. Like this:

40 + rand(8)

Pieter Jongsma
  • 3,365
  • 3
  • 27
  • 30
2

You can try this one rand(40..47)

Said Kaldybaev
  • 9,380
  • 8
  • 36
  • 53