I tried this and never got an error. Can you display the rest of the code in how you are using it? This is how I am using it public static void main(String args[]) { System.out.println(ran()); } static int ran() { double x = Math.random()*9; return (int)x; } Hope this maybe helps.