sairaseven
asked on
how to change uniformly distributed random variable to exponential or normal distribution
hi,
function rand() in C++ generates a random variable which is uniformly distributed but i want to generate a random variable that is exponentialy distributed or normally distributed. can any one tell me the code for doing this.
Saira
function rand() in C++ generates a random variable which is uniformly distributed but i want to generate a random variable that is exponentialy distributed or normally distributed. can any one tell me the code for doing this.
Saira
use time to seed the random generator
Example:
#include <time.h>
int main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
return 0;
}
#include <time.h>
int main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Axter,
the original question was not about same rand() values all the time (as usually is) but about the distribution.
The answer is NO. You may find some algorithms on internet, but you have to implement other distributions by yourself. If I want to use some approximation to normal distribution I usually make mean of several uniformly distributed values:
randVal = (rand () + rand () + rand () + rand ()) / 4 ;
This is enough for me (though it is 4x slower than just rand()). If you need more precise solution I'll try to find something else.
S.
the original question was not about same rand() values all the time (as usually is) but about the distribution.
The answer is NO. You may find some algorithms on internet, but you have to implement other distributions by yourself. If I want to use some approximation to normal distribution I usually make mean of several uniformly distributed values:
randVal = (rand () + rand () + rand () + rand ()) / 4 ;
This is enough for me (though it is 4x slower than just rand()). If you need more precise solution I'll try to find something else.
S.
ASKER
i want to generate a normally distributed random variable with given mean and variance. moreover, for exponential distribution above solution will not work. if no such function exists then please tell me the algorithm to do that.
saira
saira
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
well the solution given above don't generate normally distributed random number it just apply a transformation to a uniformly distributed random variable. same is the case with exponential. it did not work
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.