Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 10th, 2010  |  syntax: C  |  size: 1.79 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #ifndef RAND_MAX
  6. #define RAND_MAX 2147483648
  7. #endif
  8. #define N 420000
  9.  
  10. double* rdm_X(void);
  11. double* rdm_Y(void);
  12. double* rdm_Z(void);
  13.  
  14. void main(void)
  15. {
  16.    double* Random_number_list_X = rdm_X();
  17.    double* Random_number_list_Y = rdm_Y();
  18.    double* Random_number_list_Z = rdm_Z();
  19.    double X[N+1], Y[N+1], Z[N+1], density = 1, vol = 42.0;
  20.    double sum = 0, sum_x = 0, sum_y = 0, sum_z = 0;
  21.    int i;
  22.  
  23.    for (i = 0; i <= N; i++) {
  24.       X[i] = 3 * Random_number_list_X[i] + 1;
  25.       Y[i] = 7 * Random_number_list_Y[i] - 3;
  26.       Z[i] = 2 * Random_number_list_Z[i] - 1;
  27.       if ((Z[i]*Z[i]) + (sqrt(X[i]*X[i] + Y[i]*Y[i]) - 3)*(sqrt(X[i]*X[i] + Y[i]*Y[i]) - 3) <= 1) {
  28.          sum += density;
  29.          sum_x += X[i] * density;
  30.          sum_y += Y[i] * density;
  31.          sum_z += Z[i] * density;
  32.       }
  33.    }
  34.    printf("(%.5lf, %.5lf, %.5lf)\n",
  35.             sum_x/sum, sum_y/sum, sum_z/sum);
  36. }
  37.  
  38. double* rdm_X(void)
  39. {
  40.    double* Random_number_list_X = calloc(N + 1, sizeof(double));
  41.    int i;
  42.  
  43.    srand(time(NULL));
  44.    for (i = 1; i <= N; i++) {
  45.       Random_number_list_X[i] = (float) rand() / (float) RAND_MAX;
  46.    }
  47.    return Random_number_list_X;
  48. }
  49.  
  50. double* rdm_Y(void)
  51. {
  52.    double* Random_number_list_Y = calloc(N + 1, sizeof(double));
  53.    int i;
  54.    sleep(1);
  55.    srand(time(NULL));
  56.    for (i = 1; i <= N; i++) {
  57.       Random_number_list_Y[i] = (float) rand() / (float) RAND_MAX;
  58.    }
  59.    return Random_number_list_Y;
  60. }
  61.  
  62. double* rdm_Z(void)
  63. {
  64.    double* Random_number_list_Z = calloc(N + 1, sizeof(double));
  65.    int i;
  66.    sleep(2);
  67.    srand(time(NULL));
  68.    for (i = 1; i <= N; i++) {
  69.       Random_number_list_Z[i] = (float) rand() / (float) RAND_MAX;
  70.    }
  71.    return Random_number_list_Z;
  72. }