Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Generate Scilab script to check distribution of rand() % range.
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <vector>
#define square(x) ((x) * (x))
typedef unsigned int uint;
static const int trialMultipler = 15;
static int rng_out[RAND_MAX * trialMultipler];
static int rng_out_index = 0;
void generate_rng_list() {
const int rng_count = RAND_MAX * trialMultipler;
srand(time(NULL));
for (int i = 0; i < rng_count; ++i) {
rng_out[i] = rand();
}
}
void seed_rng() {
rng_out_index = 0;
}
int rng_next() {
return rng_out[rng_out_index++];
}
struct TestResult {
int modulus;
float chi2;
int degreeOfFreedom;
};
TestResult test_range(int range) {
std::vector<int> counts(range, 0);
seed_rng();
const int trials = range * trialMultipler;
for (int i = 0; i < trials; ++i) {
int n = rng_next() % range;
counts[n]++;
}
float expectedCount = trialMultipler;
float chi2 = 0.0f;
for (int i = 0; i < range; ++i) {
float chiContributor = square(counts[i] - expectedCount) / expectedCount;
chi2 += chiContributor;
}
TestResult rv;
rv.modulus = range;
rv.chi2 = chi2;
rv.degreeOfFreedom = range - 1;
return rv;
}
void write_output(const std::vector<TestResult>& results) {
FILE * fp = fopen("out.sci", "w");
if (!fp) {
fprintf(stderr, "Failed to open output file.\n");
exit(1);
}
fprintf(fp, "chi2 = [\n");
for (uint i = 0; i < results.size(); ++i) {
if (results[i].degreeOfFreedom > 0) {
fprintf(fp, "%.4f, \n", results[i].chi2);
}
else {
fprintf(stderr, "ERROR: df for mod %d is %d\n", results[i].modulus, results[i].degreeOfFreedom);
}
}
fprintf(fp, "];\n");
fprintf(fp, "df = [\n");
for (uint i = 0; i < results.size(); ++i) {
if (results[i].degreeOfFreedom > 0) {
fprintf(fp, "%d, \n", results[i].degreeOfFreedom);
}
}
fprintf(fp, "];\n");
fprintf(fp, "p = distfun_chi2cdf(chi2, df, %%F);\n");
fprintf(fp, "plot(df, p);\n");
}
int main() {
generate_rng_list();
printf("%d\n", RAND_MAX);
std::vector<TestResult> results;
results.reserve(RAND_MAX);
for (int i = 2; i <= RAND_MAX; ++i) {
results.push_back(test_range(i));
}
write_output(results);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment