Skip to content

Instantly share code, notes, and snippets.

@bakaiadam

bakaiadam/a.cpp

Created Oct 18, 2018
Embed
What would you like to do?
probability calculator
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
double rand_double()
{
return std::rand()/(double)RAND_MAX;
}
int occurence_random_from_vector(vector<double> d)
{
double randnow=rand_double();
int i=0;
for (i=0;i<d.size();i++)
{
if (randnow>d[i]) return i;
}
return i; //valami relative nagy szam lett, a veget levagjuk
}
int main()
{
int observation_number_of_occurences=2;
int observation_number_of_time_units=60*2;
int hypot_number_of_time_units=10;
double hypot_probability_of_zero_occurences;
int good_obs_num=0;
int good_obs_zero_occ_num=0;
while (true)
{
double random_probability = rand_double();
std::vector<double> d={random_probability};
double last_elem=random_probability;
for (int i=0;i<observation_number_of_occurences;i++)
{
last_elem*=random_probability;
d.push_back(last_elem);
}
int sum=0;
for(int i=0;i<observation_number_of_time_units;i++)
{
sum+=occurence_random_from_vector(d);
if (sum>observation_number_of_occurences) break;
}
if (sum==observation_number_of_occurences)
{
good_obs_num++;
int sum_hypot=0;
int sum2=0;
for(int i=0;i<hypot_number_of_time_units;i++)
{
sum2+=occurence_random_from_vector(d);
}
if (sum2==0)
{
good_obs_zero_occ_num++;
//std::cout<<ratio<<"\n";
}
double ratio=good_obs_zero_occ_num/(double)good_obs_num;
printf("%f %f %d %d %d\n",ratio,random_probability,good_obs_zero_occ_num,good_obs_num,sum2);
//}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment