Skip to content

Instantly share code, notes, and snippets.

@chaoxu
Created November 2, 2010 02:18
Show Gist options
  • Save chaoxu/659177 to your computer and use it in GitHub Desktop.
Save chaoxu/659177 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;
//n is the length of a car
double n;
double e = 0.0000001;
//double e = 0;
int interval(double l);
int interval(double l, double a){
a = a - floor(a);
//Length < 1.0
if(l<n-e) return 0;
double t = ((double) rand())/((double) RAND_MAX);
if(t>p){
//bad driver
t = ((double) rand())/((double) RAND_MAX)*(l-n);
return interval(t,a) + interval(l-t-n,a-t) + 1;
}else{
//good driver
if(l-a<n){
//the car has to park behind another car
//Clearly the interval l can be treated as having no lines
//break into 2 smaller pieces
return interval(l-n)+1;
}else{
t = floor(((double) rand())/((double) RAND_MAX)*floor(l-n-a));
return interval(t+a,a) + interval(l-a-t-n,1-n) + 1;
}
}
}
int interval(double l){
if(l<n-e){
return 0;
}
double t = ((double) rand())/((double) RAND_MAX);
if(t>p){
t = ((double) rand())/((double) RAND_MAX)*(l-n);
return interval(t) + interval(l-t-n) + 1;
}else{
return interval(l-n)+1;
}
}
int main(int argc, char* argv[]) {
srand(time(NULL));
double l = atof(argv[1]);
p = atof(argv[2]);
//int z = atoi(argv[3]);
double z = atof(argv[3]);
n = (1.0 / (double) z);
int itr = atoi(argv[4]);
long t=0;
l=l/z;
for(int i=0;i<itr;i++){
t+= interval(l,0);
}
cout << ((double) t )/ ((double) itr )/ l * n << endl;
return 0;
}
import sys
import string
def f(l,p,n,itr):
import os
t = os.popen("./a.out "+str(l)+" "+str(p)+" "+str(n)+" "+str(itr))
return float(t.readline())
k= 1000
t = 1/float(k)
for p in range(0,k):
print "{",p*t,",", f(5000,p*t,sys.argv[1],10000),"},"
import sys
import string
def f(l,p,n,itr):
import os
t = os.popen("./a.out "+str(l)+" "+str(p)+" "+str(n)+" "+str(itr))
return float(t.readline())
k=5000
for p in range(1,k):
print "{",p, ",", f(5000,0.5,p,10000),"},"
#!/bin/bash
python2 script.py 3 > test3.txt
python2 script.py 5 > test5.txt
python2 script2.py > size.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment