Mistři v programování na #hovnokod

Java #1028

napsano profi programatorem

/**
	 * Generates random number by "to" interval.
	 * <p>
	 * When returns 0 then is recursively called method this.
	 * </p>
	 * 
	 * @param to interval as integer
	 * @param withZero if returns zero too.
	 * 
	 * @return random number as int except for 0 when flag 'withZero' is not
	 *         set. Then returns 0 too.
	 */
	public static int getRandomNumber(int to, boolean withZero) {
		if (to == 1) {
			return to;
		}
		Random rand = new Random();
		int random = Math.abs(rand.nextInt() % to);
		if (random == 0 && !withZero) {
			return getRandomNumber(to, false);
		}
		return random;
	}

Anonymous,