Noob random number question

If I wanted a program to return a random percentage of a number, like,
3% to 5% of 237, how would I do this? I know rand(101) would return a
random number between 0 and 100, but I’m not sure how to have a random
percentage of a number returned. Help, please?

number / rand(101).to_f

On Thu, Apr 17, 2008 at 12:12 AM, Zoe P.
[email protected]

it doesn’t seem to be working… I need it to return a random percentage
of an integer. Like, if I wanted to have it return to me 3% to 5% of a
number (no more, no less), not between 1% and 100%.

No problem. This should get you the result you are looking for

number / (min + rand(max-min)).to_f

for example

100 / (3 + rand(6-3)).to_f

On Thu, Apr 17, 2008 at 12:58 AM, Zoe P.
[email protected]

sorry if I wasn’t clear when I originally posted, btw >.>;;

On Thu, Apr 17, 2008 at 12:51 AM, Zoe P.
[email protected]

The code is functional, but it’s giving me a much higher number than

3% of 100 is 3 and 5% of 100 is 5, so it should be returning me a value
of 3, 4, or 5 if the number I’m using is 100.

sorry bout that. Its late and I’m tired

(3 + rand(6-3)) / 100.to_f

On Thu, Apr 17, 2008 at 1:24 AM, Zoe P.
[email protected]

Zoe P. wrote:

If I wanted a program to return a random percentage of a number, like,
3% to 5% of 237, how would I do this? I know rand(101) would return a
random number between 0 and 100, but I’m not sure how to have a random
percentage of a number returned. Help, please?

rand(num*(max - min + 1)/100) + num*min/100

example

rand(237*(5 - 3 + 1)/100) + 237*3/100

Regards,
Park H.

thank you!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.