/** * Sets the completed pieces for the given protocol <tt>p</tt>. * * @parm percentage The percentage of the downloaded pieces, according to {@link * #getProbability()} * @param p the BitTorrent protocol */ private void choosePieces(int percentage, BitTorrent p) { double temp = ((double) p.nPieces / 100.0) * percentage; // We use a double to avoid the loss of precision // during the division operation int completed = (int) temp; // integer number of piece to set as completed // 0 if the peer is a newer p.setCompleted(completed); if (percentage == 100) p.setPeerStatus(1); int tmp; while (completed != 0) { tmp = CommonState.r.nextInt(p.nPieces); if (p.getStatus(tmp) != 16) { p.setStatus(tmp, 16); completed--; } } }