ltdanp22
asked on
How to limit numbers returned by a random number generator to multiples of .25?
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(con st int a, const int b){ return nag_random_discrete_unifor m(a, b);}
int nag_random_discrete_unifor m (const int a, const int b)
{
double f = nag_random_continuous_unif orm();
f = f * (b-a+1);
int rval = a + f;
return rval;
}
Here's the line that calls DiscreteUniformDeviate...
g_cIndividual[iThisID].Set Age(Discre teUniformD eviate (0, 65))
...I'd like to wrap the discreteUniformDeviate(0,6 5) call in something like static_cast<int>(DiscreteU niformDevi ate(0,65)) only instead of an int, it has to be a mult of .25. Other solutions are welcome too.
Daniel
Here's the code that pulls a number from a uniform distribution between 2 numbers a and b.
inline int DiscreteUniformDeviate(con
int nag_random_discrete_unifor
{
double f = nag_random_continuous_unif
f = f * (b-a+1);
int rval = a + f;
return rval;
}
Here's the line that calls DiscreteUniformDeviate...
g_cIndividual[iThisID].Set
...I'd like to wrap the discreteUniformDeviate(0,6
Daniel
Or this might make it easier, include this function:
float RoundToNearestQuarter (float num) {
int n = num * 4;
return (float)n/4;
}
float RoundToNearestQuarter (float num) {
int n = num * 4;
return (float)n/4;
}
ASKER
If I take num and mult it by 4 and then divide the prod by 4 don't I just get the original number?
The idea is to generate a random integer value between 0 and MAX_AGE*4. If the maximum age is 65, then between 0 and 260 for example.
This integer value is then divided by 4 (floating number division) to get the final age, which will be a multiple of 0.25 between 0 and MAX_AGE.
This integer value is then divided by 4 (floating number division) to get the final age, which will be a multiple of 0.25 between 0 and MAX_AGE.
>> int n = randomNum* 4;
>> randomNum=(float)n/4;
The first line multiplies the random decimal by 4 and then saves it as an integer n, which truncates the decimal portion of the number. Then the second line divides the integer by 4 and saves the new decimal portion in the randomNum variable.
For example,
if the random age is 40.372:
>> int n = randomNum* 4;
randomNum = 40.372 and n = 161, because 40.372*4=161.488 and the integer portion is 161
randomNum=(float)n/4;
161/4=40.25, which is 40.372 rounded to the nearest 0.25
>> randomNum=(float)n/4;
>> randomNum=(float)n/4;
The first line multiplies the random decimal by 4 and then saves it as an integer n, which truncates the decimal portion of the number. Then the second line divides the integer by 4 and saves the new decimal portion in the randomNum variable.
For example,
if the random age is 40.372:
>> int n = randomNum* 4;
randomNum = 40.372 and n = 161, because 40.372*4=161.488 and the integer portion is 161
randomNum=(float)n/4;
161/4=40.25, which is 40.372 rounded to the nearest 0.25
>> randomNum=(float)n/4;
ASKER
what does the float in parentheses do? forgive my complete ingnorance. i didn't write this code. i just have to make some mods.
ASKER
would this do the trick?
(note 0.75 is the upper limit on the first age group)
g_cIndividual[iThisID].Set Age(float( (static_ca st<int>(Di screteUnif ormDeviate (0, 4*0.75))))/4)
(note 0.75 is the upper limit on the first age group)
g_cIndividual[iThisID].Set
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
int n = randomNum* 4;
randomNum=(float)n/4;