fork download
  1. #include <math.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Vec3d
  6. {
  7. public:
  8. double x, y, z;
  9.  
  10. public:
  11. Vec3d() {};
  12. Vec3d(double a, double b, double c) : x(a), y(b), z(c) {}
  13. };
  14.  
  15. double randf()
  16. {
  17. return ((double)rand()/(double)(RAND_MAX));
  18. }
  19.  
  20. Vec3d vRand()
  21. {
  22. double theta = 2.0 * M_PI * randf();
  23. double phi = acos(2.0 * randf() - 1.0);
  24. double sinphi = sin(phi);
  25. return Vec3d(cos(theta) * sinphi, sin(theta) * sinphi, cos(phi));
  26. }
  27.  
  28. int main()
  29. {
  30.  
  31. Vec3d v = vRand();
  32.  
  33. printf("x = %f\n", v.x);
  34. printf("y = %f\n", v.y);
  35. printf("z = %f\n", v.z);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
x = 0.524709
y = -0.824658
z = -0.211234