Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- using namespace std;
- void zadanie1() {
- int zad1_tab[4] = { 0, 0, 0, 0 };
- float probabilities[] = { 0.1, 0.2, 0.4, 0.3 };
- float dystrybuanta[4] = { 0 };
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j <= i; j++) {
- dystrybuanta[i] += probabilities[j];
- }
- }
- for (auto __i = 0; __i < 100000; __i++) {
- float random = (double)rand() / (double)RAND_MAX;
- for (auto __j = 0; __j < 4; __j++) {
- if (random < dystrybuanta[__j]) {
- zad1_tab[__j]++;
- break;
- }
- }
- }
- cout << "Zadanie 1" << endl;
- for (auto __j = 0; __j < 4; __j++) {
- cout << __j+1 << ": " << zad1_tab[__j] << endl;
- }
- }
- float funkcja(float y) {
- /*
- F(x) = ax+b
- F(50) = 0
- F(150) = 1
- 50a+b = 0
- 150a+b = 1
- 150a +3b = 0
- 150a + b = 1
- 2b = -1 => b = -1/2 => 50a = 1/2 => a = 1/100
- y = 1/100 * x - 1/2
- y + 1/2 =x/100
- x = 100(y+1/2) = 100y + 50
- */
- return 100 * y + 50;
- }
- void zadanie2() {
- int zad2_tab[10] = { 0 };
- int granice[] = { 60,70,80,90,100,110,120,130,140,150 };
- for (auto __it = 0; __it < 100000; __it++) {
- float random = (double)rand() / (double)RAND_MAX;
- float x = funkcja(random);
- for (int __j = 0; __j < 10; __j++) {
- if (x < granice[__j]) {
- zad2_tab[__j]++;
- break;
- }
- }
- }
- cout << "Zadanie 2" << endl;
- for (auto __j = 0; __j < 10; __j++) {
- cout << __j + 1 << ": " << zad2_tab[__j] << endl;
- }
- }
- float f(float x) {
- return 0.0002*x - 0.01;
- }
- void zadanie3() {
- /*
- a = 100
- P = 1
- P = ah/2 => h = 2P/a => h = 2*1/100=2/100=1/50
- */
- float fmax = 0.02;
- float d = fmax + fmax * 0.01;
- int success = 0;
- int zad3_tab[10] = { 0 };
- int granice[] = { 60,70,80,90,100,110,120,130,140,150 };
- while (success != 100000) {
- float y = d * ((double)rand() / (double)RAND_MAX);
- float x = ((double)rand() / (double)RAND_MAX) * 100 + 50;
- if (f(x) > y) {
- success++;
- for (int __j = 0; __j < 10; __j++) {
- if (x < granice[__j]) {
- zad3_tab[__j]++;
- break;
- }
- }
- }
- }
- cout << "Zadanie 3" << endl;
- for (auto __j = 0; __j < 10; __j++) {
- cout << __j + 1 << ": " << zad3_tab[__j] << endl;
- }
- }
- int main()
- {
- srand(time(NULL));
- zadanie1();
- zadanie2();
- zadanie3();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement