At the end of SS.3.01 Auto-Random Colour Picker with Expressions, I concluded that the expression I wrote would need modification, since true randomness brings about the possibility of the same colour being picked twice. The time I spend rectifying this by selecting and modifying individual layers meant that this expression could not realistically be used as a time saving device.
Today in the open coding session I requested that Spencer show us how to write a code that would generate a random number but while also storing the number it picked previously, ensuring that the same number is not picked twice in a row. This is the code:
There are two arrays in this code (If we think of arrays like filing cabinets, this makes a lot more sense).
Line 1: The first array is called stuff and it contains all of the seven options.
Line 2: The second array is called used and will only contain 1’s and 0’s. The used array is define to always be the same size as the stuff array.
Line 3: A new integer called numpicked keeps track of how many times we have picked an option out of the stuff array. We will use this later to check if it is more than seven, and reset the used drawer if it is.
Line 4: Int total is just for the user to define how many random choices the program has to make.
Line 6: Void setup() is used because we do NOT need to repeat the whole function. The main repetition power comes from the for loop on line 14. Even this loop has a limit, which is defined on line 4 and called in the loop’s condition.
Line 8: Set numpicked to zero.
Line 10: This for loop then runs through completely, from lop=0 to lop=7. It goes through all seven ‘drawers’ of the used array ‘filing cabinet’ and putting a value of 0 into each one.
Line 14: Here, a large for loop begins, using lop2 this time. Every time lop2 increases in value, the following happens…
Line 15: A random number between 1-7 is picked and stored locally as thisran.
Line 16: A while loop is used to see if the used ‘drawer’ that correlates to the number thisran contains a 1. If it does, a new thisran is picked. (A while loop is used instead of an if statement because it repeats until used[thisran] does not equal 1 – meaning we have found an unused value.)
Line 19: When we are happy with thisran, we elect to use it. A number has been chosen, so numpicked increases by 1.
Line 21: If this increase in numpicked bring us to numpicked = 6, we know that the number we have just picked will be the last in the sequence (see paragraph below). Line 22: If this is the case, numpicked is reset to equal 0.
Line 23: The for loop from line 10 is run through again, setting all the ‘drawers’ in the used array to 0.
Line 27: On this clean slate, the ‘drawer’ in the used array that corresponds to thisran is given a value of 1.
Line 28: The ‘drawer’ in the stuff array that corresponds to thisran is printed.
This code actually picks out numbers in groups of six before resetting to do another six, I originally intended it to be 7, so that all numbers would be scrambled but still equally represented. Increasing the condition of the if statement on line 21 to (numpicked == stuff.length) meant that regardless of the value I plugged into total, I would still only get 13 values. I could investigate this and spend time fixing it, but I have elected not to, since I believe that, having one less space than there are options brings about another element of randomness. There is no guarantee that any one choice will be picked at all, and they all have a 1 in seven chance every time 6 options are picked.
If I can later figure out how to change this, I might add a button into my script that allows the user to pick between these two modes, scrambled or random.