Create a random number between 1 to 10 excluding 3, 5, and 9.?
This could be done by creating a random number and checking it if it doesn’t belong to 3, 5 and 9.
Random random=new Random();
int intVal=random.nextInt(11);
while(intVal==3 || intVal==5 || intVal==9){
intVal=random.nextInt(11);
}
System.out.println(intVal);
Secondly we can have an array initialized with [1,2,4,6,7,8,10] and now generate a number between 0 to 6 and return the element corresponding to that index.
May 5, 2008 | Filed Under Algorithm