1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <math.h>
  6. #define NUM_POINTS 10000
  7.  
  8. using namespace std;
  9.  
  10. const long double PI = 4.0 * atan (1.0);
  11. ofstream out;
  12. long double data[NUM_POINTS][6];
  13.  
  14. //return random long double between 0 and number specified
  15. //changed denominator to RAND_MAX + 1, supposedly more accurate
  16. long double rand (long double max){
  17. return (long double) rand () / ((long double) (RAND_MAX+1.0)) * max;
  18. }
  19.  
  20. //generates data for distribution
  21. //also calculates and prints <v_x>, <v_y>, <v_z>
  22. void generate (){
  23. long double x_total = 0;
  24. long double y_total = 0;
  25. long double z_total = 0;
  26.  
  27.  
  28. //generating theta in column 0 of matrix
  29. for(int i = 0; i < NUM_POINTS; ++i)
  30. data[i][0] = rand(2*PI);
  31.  
  32. //generating phi's in column 1 of matrix
  33. for (int i = 0; i < NUM_POINTS; ++i){
  34. for (data[i][1] = rand(1); rand(1) > 2*acos(pow(data[i][1],.5));)
  35. data[i][1] = rand(1);
  36. }
  37.  
  38. //generating speeds in column 2 of matrix
  39. for (int i = 0; i < NUM_POINTS; ++i){
  40. for (data[i][2] = rand(4); rand(1) > 4*pow(PI,-.5)*pow(data[i][2],2)*exp(-pow(data[i][2],2));)
  41. data[i][2] = rand(4);
  42. }
  43.  
  44. //deriving x,y,z components in columns 3,4,5 respectively
  45. //also calculating averages for components
  46. for (int i = 0; i < NUM_POINTS; ++i){
  47. data[i][3] = data[i][2] * sin(data[i][1]) * cos(data[i][0]);
  48. data[i][4] = data[i][2] * sin(data[i][1]) * sin(data[i][0]);
  49. data[i][5] = data[i][2] * cos(data[i][1]);
  50. x_total += data[i][3];
  51. y_total += data[i][4];
  52. z_total += data[i][5];
  53. }
  54.  
  55.  
  56. cout << "Mean for range (0-4): " << endl
  57. << "\tx component: " << (x_total/NUM_POINTS) << endl
  58. << "\ty component: " << (y_total/NUM_POINTS) << endl
  59. << "\tz component: " << (z_total/NUM_POINTS) << endl;
  60. }
  61.  
  62. int main () {
  63. //seeding with sys time
  64. srand ((unsigned) time (0));
  65.  
  66. generate();
  67. }
  68.