Part 1:
Keep a running track record of the last few results. In the case of Rumor Mill it was the number of people who knew the rumor, which was an integer less than or equal to 50. Use each result as a digit in a number in a base large enough to accommodate the values--in the example case, base 50. Make the number of successive trial results held account for a very large number of possibilities, to (almost) rule out accidental matches. Make this a running tally, so the high-order base-50 (or whatever) digit is lopped off and a new digit is inserted at the end for each new result.
Every so often at a specified interval, store this away for comparison. But at each trial, as the tally changes, compare it to the stored one, also keep track of the number of trials that have elapsed since storing the number.
As the specified interval might be smaller than the length of the repetition cycle, each time the number of trials reaches the interval, so that the tally is stored as the comparison tally, increase the interval by some factor, say 1.7 or 2 (making sure you keep it an integer, however), so that eventually the interval will be large enough to encompass a cycle.
In the case of the Rumor Mill simulation, the variable that held the result was called p2Ct. The code added for tracking was:
Initially setting:
trackLim = 10
Then within the loop, after a new result is found:
tracker = tracker * 50
IF tracker > 390624999999999# THEN
quo = INT(tracker / 39062500000000#)
tracker = tracker - quo * 39062500000000#
' this does a MOD, or remainder function
END IF
tracker = tracker + p2Ct - 1
trackCt = trackCt + 1
IF tracker = trackMatch THEN
PRINT "---"; trackCt; "---": trackCt = 0
END IF
IF trackCt = trackLim THEN
trackMatch = tracker: trackCt = 0
trackLim = INT(trackLim * 1.7)
END IF
Part 2:
As this will require keeping track of large numbers of running tallies, it's best to put them out to a disk file. Instead of the modifications above, re-modify the program to save every tally to the file and check against the saved tally from the number of trials back matching the cycle length found in part 1. Keep count of what trial you are on, and when a match is found, report that number: that's where the numbers start cycling. Make sure the random number generator starts with the same seed it did in the run for part 1.
At the beginning of the program:
OPEN "tracker.bin" FOR BINARY AS #2
Within the loop, after each new result:
tracker = tracker * 50
IF tracker > 390624999999999# THEN
quo = INT(tracker / 39062500000000#)
tracker = tracker - quo * 39062500000000#
END IF
tracker = tracker + p2Ct - 1
trackCt = trackCt + 1
PUT #2, 8 * trackCt, tracker
IF trackCt > 194068 THEN
GET #2, 8 * (trackCt - 194068), trackMatch
END IF
IF tracker = trackMatch THEN
PRINT "---"; trackCt; "---"
END IF
Note that the 194068 above is the cycle length that had been found by implementing part 1. |