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.

Related Post

String Number conversion in JavaScript
Rounding number in javascript up to some decimal point
How to prevent browser caching
Differences between ArrayList and LinkedList
You are given a list of n numbers from 1 to n-1, with one of the numbers repeated. Findout the repeated number?

Comments

Comments are closed.

Technology