+ /* Set the delay (in microseconds) between simulation of each generation */
+ /* of the simulation, also known as the delay between calls to */
+ /* WolframAutomata_draw(), which simulates one generation per call. */
+ if (get_boolean_resource(state->dpy, "random-delay", "Boolean")) {
+ /* When randomly setting the delay, the problem is to avoid being too */
+ /* fast or too slow, as well as ensuring slower speeds are chosen */
+ /* with the same likelihood as faster speeds, as perceived by a */
+ /* human. By empirical observation, we note that for 1x1 up to 4x4 */
+ /* pixel cell sizes, values for state->delay_microsec between */
+ /* 2048 (2^11) and 16556 (2^14) produce pleasant scroll rates. To */
+ /* maintain this appearance, we bitshift state->pixel_size down until */
+ /* it is a maximum of 4x4 pixels in size, record how many bitshifts */
+ /* took place, and then shift our valid window for */
+ /* state->delay_microsec up by an equal number of bitshifts. For */
+ /* example, if state->pixel_size=9, then it takes one right shift to */
+ /* reach state->pixel_size=4. Thus, the valid window for */
+ /* state->delay_microsec becomes 4096 (2^12) up to 32768 (2^15). */
+ size_t pixel_shift_range = 1;
+ size_t pixel_size_temp = state->pixel_size;
+ while (pixel_size_temp > 4) {
+ pixel_size_temp >>= 1;
+ pixel_shift_range++;
+ }
+ /* In the below line, '3' represents the total range, namely '14-11' */
+ /* from '2^14' and '2^11' as the endpoints. Similarly, the '11' in */
+ /* the below line represents the starting point of this range, from */
+ /* the exponent in '2^11'. */
+ state->delay_microsec = 1 << ((random() % 3) + 11 + pixel_shift_range);
+ } else {
+ state->delay_microsec = get_integer_resource(state->dpy, "delay-usec", "Integer");
+ }
+ if (state->delay_microsec < 0) state->delay_microsec = 0;
+
+ /* Set the number of generations to simulate before wiping the simulation */
+ /* and re-running with new settings. */
+ if (get_boolean_resource(state->dpy, "random-num-generations", "Boolean")) {
+ /* By empirical observation, keep the product */
+ /* state->num_generations * state->pixel_size */
+ /* below 10,000 to avoid BadAlloc errors from the X server due to */
+ /* requesting an enormous pixmap. This value works on both a 12 core */
+ /* Xeon with 108 GiB of RAM and a Sun Ultra 2 with 2 GiB of RAM. */
+ state->num_generations = random() % (10000 / state->pixel_size);
+ /* Ensure selected value is large enough to at least fill the screen. */
+ /* Cast to avoid overflow. */
+ if ((long)state->num_generations * (long)state->pixel_size < state->ylim) {
+ state->num_generations = (state->ylim / state->pixel_size) + 1;
+ }
+ } else {
+ state->num_generations = get_integer_resource(state->dpy, "num-generations", "Integer");
+ }