The Daily WTF
A bit ago, Aurelia shared with us a backwards for loop. Code which wasn’t wrong, but was just… weird. Well, now we’ve got some code which is just plain wrong, in a number of ways.
The goal of the following Java code is to generate some number of random numbers between 1 and 9, and pass them off to a space-separated file.
StringBuffer buffer = new StringBuffer(); long count = 0; long numResults = GetNumResults(); while (count < numResults) { ArrayList<BigDecimal> numbers = new ArrayList<BigDecimal>(); while (numbers.size() < 1) { int randInt = random.nextInt(10); long randLong = randInt & 0xffffffffL; if (!numbers.contains(new BigDecimal(randLong)) && (randLong != 0)) { buffer.append(randLong); buffer.append(” “); numbers.add(new BigDecimal(randLong)); } System.out.println(“Random Integer: ” + randInt + “, Long Integer: ” + randLong); } outFile.writeLine(buffer.toString()); buffer = new StringBuffer(); count++; }
Pretty quickly, we get a sense that something is up, with the while
To read the full article click on the 'post' link at the top.