Skip to content

Instantly share code, notes, and snippets.

@twam
Last active December 15, 2015 05:59
Show Gist options
  • Save twam/5213071 to your computer and use it in GitHub Desktop.
Save twam/5213071 to your computer and use it in GitHub Desktop.
Non-uniform distributed random numbers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
const unsigned int N = 100;
const double MIN = 1;
const double MAX = 10;
const double EXPONENT = 2;
double f(double x) {
if (EXPONENT == -1) {
return exp(log(MAX-MIN)*x)*MIN;
} else {
return exp(log(x*(-pow(MIN,EXPONENT+1)+pow(MAX,EXPONENT+1))+pow(MIN,EXPONENT+1))/(EXPONENT+1));
}
}
int main(int argc, char** argv) {
// provide initial random seed
srand(time(NULL));
printf("# i\tuniform\tnon-uniform\n");
for (unsigned int i = 0; i < N; ++i) {
double x = drand48();
printf("%i\t%.18lg\t%.18lg\n", i, x, f(x));
}
exit(EXIT_FAILURE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment