[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

noob random number question

Zoe Phoenix

4/17/2008 4:13:00 AM

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?
--
Posted via http://www.ruby-....

9 Answers

Zundra Daniel

4/17/2008 4:21:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

number / rand(101).to_f



On Thu, Apr 17, 2008 at 12:12 AM, Zoe Phoenix <dark_sgtphoenix@hotmail.com>
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?
> --
> Posted via http://www.ruby-....
>
>

Zoe Phoenix

4/17/2008 4:52:00 AM

0

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%.
--
Posted via http://www.ruby-....

Zoe Phoenix

4/17/2008 4:59:00 AM

0

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

--
Posted via http://www.ruby-....

Zundra Daniel

4/17/2008 5:06:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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 Phoenix <dark_sgtphoenix@hotmail.com>
wrote:

> sorry if I wasn't clear when I originally posted, btw >.>;;
>
> --
> Posted via http://www.ruby-....
>
>

Zundra Daniel

4/17/2008 5:24:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Apr 17, 2008 at 12:51 AM, Zoe Phoenix <dark_sgtphoenix@hotmail.com>
wrote:

> 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%.
> --
> Posted via http://www.ruby-....
>
>

Zoe Phoenix

4/17/2008 5:25:00 AM

0

The code is functional, but it's giving me a much higher number than
what I had in mind.

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.
--
Posted via http://www.ruby-....

Zundra Daniel

4/17/2008 5:35:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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 Phoenix <dark_sgtphoenix@hotmail.com>
wrote:

> The code is functional, but it's giving me a much higher number than
> what I had in mind.
>
> 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.
> --
> Posted via http://www.ruby-....
>
>

Heesob Park

4/17/2008 5:41:00 AM

0

Zoe Phoenix 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 Heesob
--
Posted via http://www.ruby-....

Zoe Phoenix

4/17/2008 5:46:00 AM

0

thank you! :-D
--
Posted via http://www.ruby-....