// generate n random particles in the unit square
// writes a particle2 file to output
// can be piped into nbody program, e.g. "randsq 10 1 10 | nbody ."

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

double frand() {
    // return a random number between 0 and 1
    return rand()/32767.;
}

void main(int argc, char **argv) {
    if (argc!=4) {
	fprintf(stderr, "Usage: randsq <nparticles> <maxspeed> <mass>\n");
	exit(1);
    }
    int n = atoi(argv[1]);
    double maxspeed = atof(argv[2]);
    double mass = atof(argv[3]);
    int i;
    printf("particle2\n# randsq %d %g %g\nm %g\n", n, maxspeed, mass, mass);
    int seed = time(0);
    fprintf(stderr, "random seed=%d\n", seed);
    srand(seed);
    for (i=0; i<n; i++)
	printf("p %g %g %g %g\n",
	    frand(), frand(), maxspeed*(2*frand()-1.), maxspeed*(2*frand()-1));
}

