View previous topic :: View next topic |
Author |
Message |
Orion

Joined: 12 Jan 2007 Posts: 413 Location: Brazil
|
Posted: Fri Nov 27, 2009 3:22 pm Post subject: random() in N increments? |
|
|
Hi, is there a way to for example return a random number between 5 and 15 in 5 point increments?
Any help appreciated.
EDIT: Is that form right?
Code: |
float(float min, float max, float inc) incrandom =
{
local float i, j;
j = ceil(random()*max);
if (j < min)
return min;
i = min;
while (i < j)
{
i = i + inc;
}
return i;
};
|
_________________ There's no signature here. Stop looking for one. |
|
Back to top |
|
 |
Downsider

Joined: 16 Sep 2008 Posts: 478
|
Posted: Fri Nov 27, 2009 7:47 pm Post subject: |
|
|
Why don't you just get an integer between 0 - 2 and mutiply it by 5, then add 5? |
|
Back to top |
|
 |
Wazat
Joined: 15 Oct 2004 Posts: 732 Location: Middle 'o the desert, USA
|
Posted: Fri Nov 27, 2009 7:55 pm Post subject: |
|
|
Uncompiled and untested code below. If it isn't completely right it will hopefully help you anyway.
Code: |
float(float min, float max, float inc) incrandom =
{
float range;
range = (max - min) / inc;
range = rint(range * random());
return min + range*inc;
} |
Assuming min and max are both increments of inc, this will give you what you want. It's up to the user to pass in valid data. You could add some code to the top of the function to ensure inc divides evenly, if you have a modulus function. Or just trust that min and max really are what the user wants.  _________________ When my computer inevitably explodes and kills me, my cat inherits everything I own. He may be the only one capable of continuing my work. |
|
Back to top |
|
 |
|