Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a method that returns a random number between 1 and 54, excluding the numb

ID: 3633702 • Letter: W

Question

write a method that returns a random number between 1 and 54, excluding the numbers passed in the arguement. the method header is specified as follows:

public static int getRandom(int... numbers)

i have basically got it but cannot figure out if i need to put it in a do while loop! anotherwords my program works when i run it through a for lop and if the result is equal to the array value it is indexing i just increment the value of the result by one. i know this is not correct but it works.. i would like to know how to do it the correct way. do i generate a random number in the do part of the loop. my problem is if it does generate a number that is equal to one of my numbers in the array how do i ake the whole process start over. i hope that makes sense...

Explanation / Answer

Yeah, put a main function: public static void main(String args[]) { getRandom(1,54); } Then, I don't understand if your min and max represent your range of random values (inclusive): return (int)((max - min + 1)*Math.random()) + min; Your range of random values (exclusive): return (int)((max - min - 1)*Math.random()) + min+1; Or the range is actually 1 to 54 and min and max are just two numbers you have to avoid: int number= (int)((54 - 1 + 1) * Math.random()) + 1; while(number == min || number == max) number= (int)((54 - 1 + 1) * Math.random()) + 1; return number;