After a lengthy session, our friends came up with a code, tentatively called sphere1a.C. Having had the experience of writing the nbody_sh1.C code, it was not to heard to write the bulk of the code, with the main driver, the output routine, and so on. The only really new ingredients were the random number generator and the function sphere that gave all particles their initial conditions.
We list here the full code for sphere.C, split up into its functions. First the information at the head of the file:
This comment block is followed by the #include directives and other general definitions, and then the declarations of all functions.
In the main function, the reader is asked to provide a value for the desired number of particles, and optionally a seed for the random number generator. When you want to do a number of independent runs, it is better not to specify such a seed, since then each run will receive a different random seed. If then you want to rerun a particular run, you can specify the seed that happened to be used in that run. You can find the value of the seed used at the beginning of the output of each run, so even if you did not specify one, you will still be able to run any run again.
Note that in reality there is no such thing as a truly random number; instead we use a pseudo-random number, generated by an algorithm designed to generate numbers that are (hopefully) random-look-alike enough for our purpose. As a technical detail, two runs will only get a different random seed as long as they start more than one second apart, since the seed is constructed from the unix clock, read off in seconds.
The following function is similar to what we used in the previous chapter, for nbody_sh1.C.
Here is a pseudo-random number generator, which we took from the old Unix example, one which returns a real number between 0 and 1.
This more general version of a pseudo-random number generator allows specification of the upper and lower bounds of the interval.
The following function does all the work, places all particles
homogeneously distributed in a sphere with radius 1.
The resulting -body system is then ready for output.