Java tutorial
//package com.java2s; import java.util.List; import java.util.Random; public class Main { private static void addAnswer(List<Integer> answers, int count) { int val = randInt(1, 2 * 9); if (!answers.contains(val)) { answers.add(val); count--; } if (count > 0) { addAnswer(answers, count); } } public static int randInt(int min, int max) { // Usually this can be a field rather than a method variable Random rand = new Random(); // nextInt is normally exclusive of the top value, // so add 1 to make it inclusive int randomNum = rand.nextInt((max - min) + 1) + min; return randomNum; } }