Inside3D!
     

random() in N increments?

 
Post new topic   Reply to topic    Inside3d Forums Forum Index -> QuakeC Programming
View previous topic :: View next topic  
Author Message
Orion



Joined: 12 Jan 2007
Posts: 413
Location: Brazil

PostPosted: Fri Nov 27, 2009 3:22 pm    Post subject: random() in N increments? Reply with quote

Hi, is there a way to for example return a random number between 5 and 15 in 5 point increments?

Any help appreciated. Smile


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
View user's profile Send private message
Downsider



Joined: 16 Sep 2008
Posts: 478

PostPosted: Fri Nov 27, 2009 7:47 pm    Post subject: Reply with quote

Why don't you just get an integer between 0 - 2 and mutiply it by 5, then add 5?
Back to top
View user's profile Send private message
Wazat



Joined: 15 Oct 2004
Posts: 732
Location: Middle 'o the desert, USA

PostPosted: Fri Nov 27, 2009 7:55 pm    Post subject: Reply with quote

Uncompiled and untested code below. If it isn't completely right it will hopefully help you anyway. Smile

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. Smile
_________________
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
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Inside3d Forums Forum Index -> QuakeC Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2004 phpBB Group