troubleshooting Question

How to limit numbers returned by a random number generator to multiples of .25?

Avatar of ltdanp22
ltdanp22Flag for United States of America asked on
C++
9 Comments2 Solutions261 ViewsLast Modified:
I'm randomly assigning individuals in a simulation ages and I need to limit the ages to numbers that are multiples of .25 (i.e. you can be 22.75 yrs. old).

Here's the code that pulls a number from a uniform distribution between 2 numbers a and b.

inline int DiscreteUniformDeviate(const int a, const int b){ return nag_random_discrete_uniform(a, b);}
int nag_random_discrete_uniform (const int a, const int b)
{
      double f = nag_random_continuous_uniform();
      f = f * (b-a+1);
      int rval = a + f;
      return rval;
}

Here's the line that calls DiscreteUniformDeviate...

g_cIndividual[iThisID].SetAge(DiscreteUniformDeviate (0, 65))

...I'd like to wrap the discreteUniformDeviate(0,65) call in something like static_cast<int>(DiscreteUniformDeviate(0,65)) only instead of an int, it has to be a mult of .25. Other solutions are welcome too.

Daniel
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros