-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
shibly
  • 11,690
  • 35
  • 99
  • 163
  • 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,335
  • 3
  • 24
  • 29
2

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

Said Kaldybaev
  • 8,730
  • 8
  • 35
  • 53