The Daily WTF
Follow
CodeSOD: Plenty of IDs
Venkat joined a team engaged in a refactoring effort, uncovering logical flaws in their existing code. One particular Python function, 'generate_random_number', was designed to produce a unique five-digit identifier for VM provisioning. It utilizes 'random.sample' to select five unique digits from 0-9. This method generates numbers without repeating digits, limiting the possible combinations. The output is then converted to a string. While seemingly sufficient, this approach significantly reduces the potential identifier space. The actual number of unique identifiers is restricted to approximately 30,240, far less than a typical five-digit range. This constraint leads to a surprisingly high probability of collisions. Due to the birthday problem, there's a 1% chance of a collision after only 26 requests. The probability of a collision escalates to 50% after just 206 requests. Therefore, 30,240 unique values are likely insufficient for robust VM provisioning, even for smaller-scale operations. This highlights a critical flaw in the identifier generation logic.