Skip to content

Instantly share code, notes, and snippets.

@brunophilipe
Last active August 29, 2015 14:05
Show Gist options
  • Save brunophilipe/d483b627d6fdcc11f2ba to your computer and use it in GitHub Desktop.
Save brunophilipe/d483b627d6fdcc11f2ba to your computer and use it in GitHub Desktop.
People Generator
Generating 10000000 People
Range Count Calcul. Actual Deviat.
0-4 682781 6.83 6.82 -0.01
5-9 729411 7.29 7.30 +0.01
10-14 729765 7.30 7.30 +0.00
15-19 718607 7.19 7.18 -0.01
20-24 673966 6.74 6.74 +0.00
25-29 689531 6.90 6.89 -0.01
30-34 728300 7.28 7.29 +0.01
35-39 806730 8.07 8.07 +0.00
40-44 797272 7.97 7.97 -0.00
45-49 715718 7.16 7.14 -0.02
50-54 623947 6.24 6.25 +0.01
55-59 478314 4.78 4.78 -0.00
60-64 383642 3.84 3.84 +0.00
65-69 338004 3.38 3.38 -0.00
70-74 314766 3.15 3.15 +0.00
75-79 263560 2.64 2.63 -0.01
80-84 175697 1.76 1.76 +0.00
85+ 149989 1.50 1.51 +0.01
Average Deviation: +0.0000
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define PEOPLE_COUNT 10000000
#define PEOPLE_COUNT_F (float)(PEOPLE_COUNT/100.f)
typedef struct t_person
{
short age;
short age_range;
} t_person;
int main(int argc, char const *argv[])
{
srand(time(0));
char *_ageRanges[18] = {"0-4", "5-9", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74", "75-79", "80-84", "85+"};
float _ageDistribution[18] = {6.82, 7.3, 7.3, 7.18, 6.74, 6.89, 7.29, 8.07, 7.97, 7.14, 6.25, 4.78, 3.84, 3.38, 3.15, 2.63, 1.76, 1.51};
t_person *people = malloc(sizeof(t_person) * PEOPLE_COUNT);
printf("Generating %d People\n", PEOPLE_COUNT);
// Generating people
for (long person = 0; person < PEOPLE_COUNT; ++person)
{
float accumulator = 0.f;
int entropy = rand()%10000;
for (int range = 0; range < 18; ++range)
{
accumulator += _ageDistribution[range]*100;
if (accumulator >= entropy) // Pick age
{
people[person].age_range = range;
people[person].age = (range * 5) + rand()%4;
break;
}
}
}
// Calculating Statistics
int actualCount[18];
float actualDistribution[18];
memset(&actualCount, '\0', sizeof(actualCount));
for (long person = 0; person < PEOPLE_COUNT; ++person)
{
actualCount[people[person].age / 5]++;
}
printf("Range\tCount\t\tCalcul.\tActual\tDeviat.\n");
float totalDeviation = 0.0f;
for (int range = 0; range < 18; ++range)
{
actualDistribution[range] = actualCount[range]/PEOPLE_COUNT_F;
float deviation = _ageDistribution[range]-actualDistribution[range];
totalDeviation += deviation;
printf("%s\t%d\t\t%.2f\t%.2f\t%+.2f\n", _ageRanges[range], actualCount[range], actualDistribution[range], _ageDistribution[range], deviation);
}
printf("Average Deviation: %+.4f\n", totalDeviation/18.f);
// LOL
free(people);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment