/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.c
* Copyright (C) DMGualtieri 2011
*
* one_over_f is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* one_over_f is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*/
/*
Uses dice throws to generate 1/F noise (Voss algorithm)
Reference: Martin Gardner, "Fractal Music, Hypercards and more...,"
W.H. Freeman and Company, New York, 1992, pp. 12-18.
Voss dice algortithm:
1) Take ten dice. Identify each die with a bit position b
of a binary number R, which is the roll number.
2) Roll all dice and sum. This is R = 0, and the sum is your output.
3) Increment R and roll only those dice whose bit position
satisfies the condition R XOR (R-1); that is, roll only if the bit
identified with the die has changed.
4) Sum all dice. The sum is your output.
5) Go to step 3.
*/
// Note - Written more for readability than speed!
#include
#include
#include
#include
long int i,j, iterations;
int dice[10];
int sum;
FILE *outdata;
//generates a random integer between 0 and face
int throw_die(int face)
{
return ( rand() % face);
}
int main(int argc, char *argv[])
{
if (argc<1)
{
printf("Usage: one_over_f number_of_iterations\n");
exit(1);
}
//Seed random number generator
srand ( (unsigned)time ( NULL ) );
//Open output datafile
if ((outdata = fopen("output_data.txt","w"))==NULL)
{printf ("\nOutput file cannot be opened.\n");
exit (1);}
else
{
printf("Output file = output_data.txt\n");
}
iterations = (long int)atoi(argv[1]);
printf("iterations = %ld\n",iterations);
//initial throw
for(i=0;i<10;i++)
{
dice[i] = throw_die(6);
}
for(j=1;j