Skip to content

Instantly share code, notes, and snippets.

@chaoxu
Created October 12, 2010 03:02
Show Gist options
  • Save chaoxu/621604 to your computer and use it in GitHub Desktop.
Save chaoxu/621604 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <time.h>
#include "iostream"
#include <math.h>
using namespace std;
double p;
int interval(double l, double a){
a = a - floor(a);
if(l<1.0) return 0;
double t = ((double) rand())/((double) RAND_MAX);
if(t>p || l-a<1.0){
t = ((double) rand())/((double) RAND_MAX)*(l-1.0);
return interval(t,a) + interval(l-t-1.0,a-t) + 1;
}else{
t = floor(((double) rand())/((double) RAND_MAX)*floor(l-1.0-a));
return interval(t+a,a) + interval(l-a-t-1,0) + 1;
}
}
int main(int argc, char* argv[]) {
srand(time(NULL));
double l = atof(argv[1]);
p = atof(argv[2]);
int itr = atoi(argv[3]);
long t=0;
for(int i=0;i<itr;i++){
t+= interval(l,0);
}
cout << ((double) t )/ ((double) itr )/ l << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment