Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <math.h>
- using namespace std;
- void recursion();
- void uniform();
- void exponent();
- int main()
- {
- cout << "Recursion method:" << endl;
- recursion();
- cout << endl << endl << "Uniform distribution:" << endl;
- uniform();
- cout << endl << endl << "Exponential distribution:" << endl;
- exponent();
- system("pause");
- return 0;
- }
- void recursion()
- {
- double Mat, sum = 0;
- double Z[10], x[10];
- double x1[10], q;
- int n = 10;
- int Z_1 = 12345,
- Z0 = 97531,
- a0 = 1,
- a1 = 1,
- M = 5000;
- cout << "Z_1 = " << Z_1 << endl;
- cout << "Z0 = " << Z0 << endl;
- cout << "a0 = " << a0 << endl;
- cout << "a1 = " << a1 << endl;
- cout << "M = " << M << endl;
- Z[0] = Z_1;
- Z[1] = Z0;
- for (int i = 2; i < n; i++)
- {
- Z[i] = a0*Z[i - 2] + a1*Z[i - 1];
- }
- cout << endl;
- for (int i = 0; i < n; i++)
- {
- x[i] = fmod(Z[i], M);
- q = x[i];
- x1[i] = q / M;
- cout << "Z[" << i + 1 << "] = " << Z[i]
- << setw(8) << "x[" << i + 1 << "] = " << x[i] << ";"
- << setw(8) << "x1[" << i + 1 << "] = " << x1[i] << endl;
- }
- for (int i = 0; i < n; i++)
- {
- sum = sum + x1[i];
- cout << sum;
- }
- }
- void uniform()
- {
- int n = 10;
- double r[10], x[10];
- int b = rand(), a = rand() < b;
- cout << "a = " << a << ";" << setw(8) << "b = " << b << endl;
- for (int i = 0; i < 10; i++)
- {
- r[i] = ((double)rand() / (RAND_MAX));
- x[i] = a + (b - a)*r[i];
- cout << "R[" << i+1 << "] = " << r[i] << setw(8) << "x = " << x[i] << endl;
- }
- }
- void exponent()
- {
- int n = 10;
- double r[10], x[10];
- double jl = 0.8;
- for (int i = 0; i < 10; i++)
- {
- r[i] = ((double)rand() / (RAND_MAX));
- x[i] = (-1 / jl) * log(r[i]);
- cout << "R[" << i + 1 << "] = " << r[i] << setw(8) << "x = " << x[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement