Guest User

Untitled

a guest
Jan 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <complex.h>
  5.  
  6. double rUniform(double, double);
  7. double complex rNormal(double, double);
  8.  
  9. const int ITMAX=1000;
  10.  
  11. int main(){
  12. srand(time(NULL));
  13.  
  14. for( int i=0; i < ITMAX; i++){
  15. complex double z = rNormal(5,1);
  16. printf("%ft%fn", creal(z),cimag(z));
  17. }
  18. }
  19.  
  20. double rUniform(double a, double b){
  21. return rand()*(b-a)/RAND_MAX+a;
  22. }
  23.  
  24. double complex rNormal(double mu, double sigma){
  25. // Generate a pair of random numbers which are
  26. // sampled from a uniform distribution on the
  27. // interval [0,1].
  28. double u1 = rUniform(0,1);
  29. double u2 = rUniform(0,1);
  30.  
  31. double pi = acos(-1); // Lazy way to get pi=3.14... from arccos(-1)
  32.  
  33. // Generte samples for the variables Theta and T by
  34. // applying the inverses of the cumulative distribution
  35. // functions to uniform random variables.
  36. double theta = 2*pi*u1;
  37. double t = -log(1-u2);
  38.  
  39.  
  40. // Generate r from t, recall that t=r^2/(2*sigma^2)
  41. // so we can get r from sqrt(2*sigma^2*t)
  42. double r = sqrt(2*sigma*sigma*t);
  43.  
  44. // Generate the cartesian coordinates x and y from the polar
  45. // coordinates r and theta. Recall that these x and y are the
  46. // variables which actually obey N(mu,sigma).
  47. double x = r*cos(theta);
  48. double y = r*sin(theta);
  49.  
  50. // We can return the pair in a complex variable z.
  51. double complex z = (x+mu)+I*(y+mu);
  52.  
  53. return z;
  54. }
Add Comment
Please, Sign In to add comment