Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. void recursion();
  8. void uniform();
  9. void exponent();
  10.  
  11. int main()
  12. {
  13. cout << "Recursion method:" << endl;
  14. recursion();
  15. cout << endl << endl << "Uniform distribution:" << endl;
  16. uniform();
  17. cout << endl << endl << "Exponential distribution:" << endl;
  18. exponent();
  19. system("pause");
  20. return 0;
  21. }
  22.  
  23. void recursion()
  24. {
  25. double Mat, sum = 0;
  26. double Z[10], x[10];
  27. double x1[10], q;
  28. int n = 10;
  29. int Z_1 = 12345,
  30. Z0 = 97531,
  31. a0 = 1,
  32. a1 = 1,
  33. M = 5000;
  34. cout << "Z_1 = " << Z_1 << endl;
  35. cout << "Z0 = " << Z0 << endl;
  36. cout << "a0 = " << a0 << endl;
  37. cout << "a1 = " << a1 << endl;
  38. cout << "M = " << M << endl;
  39.  
  40. Z[0] = Z_1;
  41. Z[1] = Z0;
  42. for (int i = 2; i < n; i++)
  43. {
  44. Z[i] = a0*Z[i - 2] + a1*Z[i - 1];
  45. }
  46.  
  47. cout << endl;
  48. for (int i = 0; i < n; i++)
  49. {
  50. x[i] = fmod(Z[i], M);
  51. q = x[i];
  52. x1[i] = q / M;
  53. cout << "Z[" << i + 1 << "] = " << Z[i]
  54. << setw(8) << "x[" << i + 1 << "] = " << x[i] << ";"
  55. << setw(8) << "x1[" << i + 1 << "] = " << x1[i] << endl;
  56. }
  57.  
  58. for (int i = 0; i < n; i++)
  59. {
  60. sum = sum + x1[i];
  61. cout << sum;
  62. }
  63. }
  64.  
  65. void uniform()
  66. {
  67. int n = 10;
  68. double r[10], x[10];
  69. int b = rand(), a = rand() < b;
  70. cout << "a = " << a << ";" << setw(8) << "b = " << b << endl;
  71. for (int i = 0; i < 10; i++)
  72. {
  73. r[i] = ((double)rand() / (RAND_MAX));
  74. x[i] = a + (b - a)*r[i];
  75. cout << "R[" << i+1 << "] = " << r[i] << setw(8) << "x = " << x[i] << endl;
  76. }
  77. }
  78.  
  79. void exponent()
  80. {
  81. int n = 10;
  82. double r[10], x[10];
  83. double jl = 0.8;
  84.  
  85. for (int i = 0; i < 10; i++)
  86. {
  87. r[i] = ((double)rand() / (RAND_MAX));
  88. x[i] = (-1 / jl) * log(r[i]);
  89. cout << "R[" << i + 1 << "] = " << r[i] << setw(8) << "x = " << x[i] << endl;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement