Skip to content

Instantly share code, notes, and snippets.

@dyigitpolat
Created April 28, 2018 13:14
Show Gist options
  • Select an option

  • Save dyigitpolat/0228a2e45eca50fafa9c6a30eb304f8a to your computer and use it in GitHub Desktop.

Select an option

Save dyigitpolat/0228a2e45eca50fafa9c6a30eb304f8a to your computer and use it in GitHub Desktop.
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#define N 5000000L
float rand_1()
{
float x = (float)rand()/(float)(RAND_MAX/1);
}
float line_avg()
{
float total_dist = 0;
for(long i = 0; i < N; i++)
{
float loc1 = rand_1();
float loc2 = rand_1();
float dist = loc1-loc2;
dist = (dist<0 ? -dist : dist);
total_dist += dist;
}
total_dist /= N;
}
float sq_avg()
{
float total_dist = 0;
for(long i = 0; i < N; i++)
{
float loc1x = rand_1();
float loc2x = rand_1();
float loc1y = rand_1();
float loc2y = rand_1();
float dist = (loc1x-loc2x)*(loc1x-loc2x)+
(loc1y-loc2y)*(loc1y-loc2y);
dist = (dist<0 ? -dist : dist);
total_dist += sqrt(dist);
}
total_dist /= N;
}
int main()
{
printf("%f\n", line_avg());
printf("%f\n", sq_avg());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment