Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iomanip>
- #include <iostream>
- #include <time.h>
- #include <math.h>
- #include <random>
- double average(double* arr, int len);
- int part_a();
- double* run_simulation(int start_cost);
- double* get_calls(double mean_time, double total_time);
- double exp_dist(double mean);
- double get_probability(int X, int N);
- int main(){
- part_a();
- //part_b()
- //part_c()
- return 0;
- }
- int part_a(){
- double* samples = static_cast<double *>(calloc(1000, sizeof(double)));
- double* run_sim_res;
- for(int i = 0; i < 1000; i++){
- //if(i % 1000 == 0)
- // printf("%d\n", i);
- run_sim_res = run_simulation(100);
- double seats_sold = run_sim_res[0];
- double profit = run_sim_res[1];
- double time_elapsed = run_sim_res[2];
- samples[i] = time_elapsed;
- }
- double avg = average(samples,1000);
- double avg_days = avg/1440.0;
- printf("Average number of days to sell 150 tickets at $100 each is: %f !", avg_days);
- //free(run_sim_res);
- return 0;
- }
- double average(double* arr, int len) {
- double sum = 0.0;
- for(int i = 0; i < len; i++) {
- sum += arr[i];
- }
- double avg = sum/len;
- return avg;
- }
- double* run_simulation(int start_cost) {
- double total_time = 60 * 24.0 * 30;
- double mean_time = 120.0;
- int seats = 150;
- int sold = 0;
- double cur_cost = start_cost;
- double profit = 0.0;
- double time_elapsed = 0.0;
- srand(time(0));
- double* calls = get_calls(mean_time,total_time);
- for(int i = 1; i < calls[0]; i++){
- time_elapsed = time_elapsed + calls[i];
- double probability = get_probability(cur_cost,sold);
- double drawn_sample = (double)rand() / (double)RAND_MAX;
- if(drawn_sample <= probability) {
- profit += cur_cost;
- sold += 1;
- if(sold == seats) {
- break;
- }
- }
- }
- double* to_ret = static_cast<double *>(calloc(3, sizeof(double)));
- to_ret[0] = sold;
- to_ret[1] = profit;
- to_ret[2] = time_elapsed;
- return to_ret;
- }
- double* get_calls(double mean_time, double total_time) {
- double* calls = static_cast<double *>(calloc(500,sizeof(double)));
- int idx = 0;
- double cur_sum = 0.0;
- while(1) {
- double val = exp_dist(mean_time);
- if(cur_sum + val > total_time){
- break;
- }
- calls[idx+1] = val;
- idx++;
- //printf("%d\n", idx);
- cur_sum += val;
- }
- calls[0] = (double) idx;
- return calls;
- }
- double exp_dist(double mean) {
- double lambda = 1.0/120.0;
- std::random_device rd;
- std::mt19937 generator(rd());
- std::exponential_distribution<double> distribution(lambda);
- double to_ret = distribution(generator);
- return to_ret;
- }
- double get_probability(int X, int N) {
- double f = (300 - exp(X/100.0))/600.0;
- double g = N/350.0;
- double p = (f > g) ? f : g;
- return p;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement