Ensuring you randomize your scanning is critically important for the success of your model building. For example, in the first scenario where we said three scans of each pill, 5 pills per bottle, 20 bottles should not be scanned in exactly that order.
The order you scan should not be 3x5x20 but rather an entirely randomized order, such as: three scans of each pill, then randomize the 5 pills from each bottle to 2 pills from one bottle and 3 pills from another, and finally 1 from the last one until you complete the 3 scans of the last of the 5 pills from the 20 bottles.
The reason you want to make sure to randomize is that environmental factors can influence the scan results, so predictive patterns might skew the spectra. When you randomize, you ensure it is the actual matter you are scanning and not the order, temperature, ambient light etc. that is measured.