fork download
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #define exponent 2.718
  6.  
  7. using namespace std;
  8. void Normal(void)
  9. {
  10. double U, V;
  11. double S, X;
  12.  
  13. do {
  14. U = rand()%200-100;
  15. V = rand()%200-100;
  16. U /= 100;
  17. V /= 100;
  18. cout << "V = " << V << "U = " << U << "\n";
  19.  
  20. S = U*U + V*V;
  21. cout << "S = " << S << "\n";
  22. if(S > 0 && S <= 1) break;
  23. }
  24. while (1);
  25.  
  26. X = U * sqrt((-2) * log(S) / S);
  27. cout << "\nResult for Normal: " << X << "\n\n";
  28. }
  29.  
  30. void Puasson(void)
  31. {
  32. double lambda, exp;
  33. double L;
  34. double p,u ;
  35. int k;
  36. lambda = 8/1;
  37. exp = exponent;
  38. L = pow(exp, (0-lambda));
  39. cout << "L = " << L << "\n";
  40. p = 1;
  41. k = 0;
  42.  
  43. do {
  44. k++;
  45. u = rand()%100;
  46. u /= 100;
  47. p *= u;
  48. } while(p > L);
  49.  
  50. k--;
  51. cout << "\nResult for Puasson: " << k << "\n\n";
  52. }
  53. int main(void)
  54. {
  55. srand(time(NULL));
  56.  
  57. //First
  58. Normal();
  59.  
  60. //Second
  61. Puasson();
  62.  
  63. system("pause");
  64. return(0);
  65. }
Success #stdin #stdout #stderr 0s 16064KB
stdin
Standard input is empty
stdout
V = 0.82U = 0.19
S = 0.7085

Result for Normal: 0.187396

L = 0.000335741

Result for Puasson: 10

stderr
sh: 1: pause: not found