Game Logic Pt 1

Hi - I’m struggling with Game Logic Pt 1. There isn’t a cheat sheet or a video to help for this project.
I have seen the other post regarding this project, but I still don’t understand what it is I’m meant to be writing.
This is the code I’ve written so far:

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

This is step 6 - 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.

and this is the following step - 
Calculate a random number between 0 and max

Use the result from task 6 to calculate a random number between 0 and max (the parameter you pass into generateRandomNumber()) and save it to a local double variable.

I don't understand how they expect us to write that. I don't know if I've copied the code correctly. Any help is greatly appreciated.


Hi, when you declare a method in java, you have to specify what return value you want to come out when it is invoked. If you write

int generateRandomNumber() { 
  //some code 
  // return something of type "int"

you have to return an int for this not to throw an error. For example

Bread generateRandomBread() { 
  // some code
  // return something of type "Bread"

In the method you wrote so far you are returning randomNum as a double which will for sure create an issue.

Hey thanks for the reply.

That’s what I thought, so I changed it from double it int, any it still doesn’t work because Math.random() creates a double.

I’m really confused and there’s no help on the main site for this project.

When you have the return type as double, what exactly isn’t working? Also, for the following step, consider this:
If I have a number x(between 0 and 1) and larger number n, and I want a number between 0 and n(inclusive). If x is 1, what would I need to do to get to n from x?

Hey thanks for helping!

This is the Error code I get when I try to use my code ( in first post): error: incompatible types: possible lossy conversion from double to int
int randomNum = Math.random();
1 error

To answer your second question, I don’t understand what you’re asking if I’m being entirely honest.

Math.random() returns a double. What was the error when you had double randomNum = Math.random();?

My second question was only meant to be thought provoking in regards to the second step

I was just trying to make you think: if you had 1, and you wanted to make it 10, what would you do to it? Likewise, using the same process, how would you make 1 into 7. That is the upper bound. Now, how would you make the lower bound, using the same process?

The error was something regarding ‘lossy’. Basically saying that I wanted to return an int and yet I was using a double.

If I had 1 and wanted 10 I’d add 9. 1 to 7 would be add 6. I guess the lower band would be to minus it.

I have managed to complete the project now. Someone else had posted some code on another forum which fixed my issue. But I still don’t understand Math.random(), so I’ll only have more issues in the future, so I appreciate you helping me understand.

I e-mailed codecademy about there not being a lesson on Math.Random() and they have linked me to a future lesson to complete. Frustrating that it’s not before we actually have to use it but I’ll post the link for anyone else who gets stuck:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.