noob random number question

Z

Zoe Phoenix

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?
 
Z

Zundra Daniel

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

number / rand(101).to_f
 
Z

Zoe Phoenix

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%.
 
Z

Zundra Daniel

[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
 
Z

Zoe Phoenix

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.
 
Z

Zundra Daniel

[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
 
H

Heesob Park

Zoe said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top