Cumulative Project 3 - Game Logic Pt 1

Hi, I’m working on the Game Logic Pt 1 Tasks in the Java course (Introduction to android with Java).
I’m stuck on this activity:

Use Math.random() to generate a random decimal value.
All Java objects have access to the built-in Math object.
And Math provides a method named random() which returns a randomly chosen double value greater than or equal to 0, but less than 1.
For example, some possible return values are 0.2312 , 0 , or 0.999999 .
Call Math.random() within generateRandomNumber() and save it to a local variable.

So far, I have done this:

public class MainActivity {

// Add generateRandomNumber() here

int generateRandomNumber(int max) {

double generateRandomNumber = Math.random();

return genrateRandomNumber;

}

// Add getGameOverMessage() here

}

But I get this error:
MainActivity.java:7: error: cannot find symbol
return genrateRandomNumber;
^
symbol: variable genrateRandomNumber
location: class MainActivity
1 error

I honestly am lost, and this specific project doesn’t have a cheat sheet. It’s the first time I’ve come across Math.random(). Any help would be incredible!

Your Code:

public class MainActivity {
  // Add generateRandomNumber() here
  int generateRandomNumber(int max) {
    double generateRandomNumber = Math.random();
    return genrateRandomNumber;
  }
  // Add getGameOverMessage() here
}

It’s poor practice to include a variable with the same name as a function in your program - especially when that variable resides inside the function of the same name.

The error thrown is a result of a spelling mistake: You defined the generateRandomNumber variable, but return genrateRandomNumber (missing an e in the variable name). You should change both references to a new variable name (perhaps randNum or similar).

oh my, what a silly thing to do facepalm.
Thanks, and thanks for the best practice advice too!

I’ve now updated the code to:

public class MainActivity {

// Add generateRandomNumber() here

int generateRandomNumber(int max) {
  double randomNumber = Math.random();
  return randomNumber;
}
// Add getGameOverMessage() here

}

What I’m still unsure about though, are the instructions - as it specifies to create generateRandomNumber as an int, but then the Math.random is a double, so when I run the code, I get this error:

MainActivity.java:7: error: incompatible types: possible lossy conversion from double to int
  return randomNumber;
         ^

So sorry, I’m just so new to this that this specific aspect hasn’t quite clicked yet :confused:

You are trying to return a double value in a method that is of type int. If the goal of the method is to return a decimal value, change the return type from int to double in the method definition.

Because generateRandomNumber specifies that an object of the int type should be returned, returning randomNumber (which is a double) will throw an error.

I don’t have PRO access so I’m not able to see the exact project requirements, but you can convert the result of Math.random() (with some additional code) to an int using type casting (more on type casting here).

Math.random() returns a positive double greater than or equal to 0.0 and less than 1.0. More on Math.random() here. Type casting a double to an int truncates the decimal portion of the double. In this way, you can use

int num = (int)(Math.random() * max + 1);

where max is the maximum value of the range in which you want your number to be generated.

More on random number generation within a range here.

2 Likes