Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <time.h>
  6. #include <math.h>
  7. #include <random>
  8.  
  9. double average(double* arr, int len);
  10. int part_a();
  11. double* run_simulation(int start_cost);
  12. double* get_calls(double mean_time, double total_time);
  13. double exp_dist(double mean);
  14. double get_probability(int X, int N);
  15.  
  16. int main(){
  17.     part_a();
  18.     //part_b()
  19.     //part_c()
  20.     return 0;
  21. }
  22.  
  23. int part_a(){
  24.     double* samples = static_cast<double *>(calloc(1000, sizeof(double)));
  25.     double* run_sim_res;
  26.     for(int i = 0; i < 1000; i++){
  27.         //if(i % 1000 == 0)
  28.         //  printf("%d\n", i);
  29.         run_sim_res = run_simulation(100);
  30.         double seats_sold = run_sim_res[0];
  31.         double profit = run_sim_res[1];
  32.         double time_elapsed = run_sim_res[2];
  33.         samples[i] = time_elapsed;
  34.     }
  35.     double avg = average(samples,1000);
  36.     double avg_days = avg/1440.0;
  37.     printf("Average number of days to sell 150 tickets at $100 each is: %f !", avg_days);
  38.     //free(run_sim_res);
  39.     return 0;
  40. }
  41.  
  42. double average(double* arr, int len) {
  43.     double sum = 0.0;
  44.     for(int i = 0; i < len; i++) {
  45.         sum += arr[i];
  46.     }
  47.     double avg = sum/len;
  48.     return avg;
  49. }
  50.  
  51. double* run_simulation(int start_cost) {
  52.     double total_time = 60 * 24.0 * 30;
  53.     double mean_time = 120.0;
  54.     int seats = 150;
  55.     int sold = 0;
  56.     double cur_cost = start_cost;
  57.     double profit = 0.0;
  58.     double time_elapsed = 0.0;
  59.     srand(time(0));
  60.  
  61.     double* calls = get_calls(mean_time,total_time);
  62.     for(int i = 1; i < calls[0]; i++){
  63.         time_elapsed = time_elapsed + calls[i];
  64.         double probability = get_probability(cur_cost,sold);
  65.         double drawn_sample = (double)rand() / (double)RAND_MAX;
  66.  
  67.         if(drawn_sample <= probability) {
  68.             profit += cur_cost;
  69.             sold += 1;
  70.             if(sold == seats) {
  71.                 break;
  72.             }
  73.         }
  74.     }
  75.  
  76.     double* to_ret = static_cast<double *>(calloc(3, sizeof(double)));
  77.     to_ret[0] = sold;
  78.     to_ret[1] = profit;
  79.     to_ret[2] = time_elapsed;
  80.     return to_ret;
  81. }
  82.  
  83. double* get_calls(double mean_time, double total_time) {
  84.     double* calls = static_cast<double *>(calloc(500,sizeof(double)));
  85.     int idx = 0;
  86.     double cur_sum = 0.0;
  87.     while(1) {
  88.         double val = exp_dist(mean_time);
  89.         if(cur_sum + val > total_time){
  90.             break;
  91.         }
  92.         calls[idx+1] = val;
  93.         idx++;
  94.         //printf("%d\n", idx);
  95.         cur_sum += val;
  96.     }
  97.     calls[0] = (double) idx;
  98.     return calls;
  99. }
  100.  
  101. double exp_dist(double mean) {
  102.     double lambda = 1.0/120.0;
  103.     std::random_device rd;
  104.     std::mt19937 generator(rd());
  105.     std::exponential_distribution<double> distribution(lambda);
  106.     double to_ret = distribution(generator);
  107.     return to_ret;
  108. }
  109.  
  110.  
  111. double get_probability(int X, int N) {
  112.     double f = (300 - exp(X/100.0))/600.0;
  113.     double g = N/350.0;
  114.     double p = (f > g) ? f : g;
  115.     return p;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement