# Creating a Secret Numbers game, need help with random number max and min.

• May 23rd, 2013, 03:15 PM
kjdeutin
Creating a Secret Numbers game, need help with random number max and min.
So I am trying to come up with a game that has three levels, easy, medium, and hard as well as a custom. The problem I have is creating a random integer that allows there to be a minimum of 1 and a max of 10 for easy, min of 1 and a max of 25 for medium, and a min of 1 and a max of 100 for hard. The custom difficulty just has a minimum but no max.

This is my method:

Code :

```public static int randInt(int max) { int min = 1;   java.util.Random rand = new java.util.Random(); return rand.nextInt(max - min) + min; }```
Any help would be great.

The number cannot be zero. It has to have a minimum of 1.
• May 23rd, 2013, 03:41 PM
jps
Re: Creating a Secret Numbers game, need help with random number max and min.
What seems to be the trouble with the posted method?
What do you seem to be stuck on?
• May 23rd, 2013, 04:58 PM
kjdeutin
Re: Creating a Secret Numbers game, need help with random number max and min.
I don't think that it will return the maximum value. For example if I use 10, it will never return 10.
• May 23rd, 2013, 06:13 PM
Zaphod_b
Re: Creating a Secret Numbers game, need help with random number max and min.
Quote:

Originally Posted by kjdeutin
I don't think that it will return the maximum value....

I don't it will either. In fact I know it won't.

Consider:

If you call nextInt(10), you get numbers 0, 1, ..., 9, so nextInt(10)+1 gives a number in the range 1, 2, ..., 10. Right?

If you call nextInt(25), you get numbers 0, 1, ..., 24, so nextInt(25)+1 gives a number in the range 1, 2, ..., 25. Right?

If you call nextInt(100), you get numbers 0, 1, ..., 99, so nextInt(100)+1 gives a number in the range 1, 2, ..., 100. Right?

Now here's the Biggie:

Suppose you want a function f(n) that returns a number in the range 1, 2, ..., n

Then inside the function:
• What argument do you use in calling nextInt?

• What do you do to the result returned from nextInt to obtain a value in the specified range?

Cheers!

Z
• May 23rd, 2013, 07:00 PM
kjdeutin
Re: Creating a Secret Numbers game, need help with random number max and min.
Quote:

Originally Posted by Zaphod_b
I don't it will either. In fact I know it won't.

Consider:

If you call nextInt(10), you get numbers 0, 1, ..., 9, so nextInt(10)+1 gives a number in the range 1, 2, ..., 10. Right?

If you call nextInt(25), you get numbers 0, 1, ..., 24, so nextInt(25)+1 gives a number in the range 1, 2, ..., 25. Right?

If you call nextInt(100), you get numbers 0, 1, ..., 99, so nextInt(100)+1 gives a number in the range 1, 2, ..., 100. Right?

Now here's the Biggie:

Suppose you want a function f(n) that returns a number in the range 1, 2, ..., n

Then inside the function:
• What argument do you use in calling nextInt?

• What do you do to the result returned from nextInt to obtain a value in the specified range?

Cheers!

Z

Thanks. I'm really new to Java so it might be hard for me to understand some of the things you mention.

This is how I think I used this argument to call it:

Code :

`randomNumber = randInt(max + 1);`

Does that help with what you were asking? Can you clarify the "What do you do to the result returned.." question?

I also have this:

Code :

```if ( userChoice == 1 ) { System.out.println("You have chosen level Easy. I will select a number between 1 and 10, inclusive." + " You have 3 tries to guess my number!"); guesses = 3; max = 10; } // This else if statement confirms the difficulty level Medium, and gives the user instructions else if( userChoice == 2) { System.out.println("You have chosen level Medium. I will select a number between 1 and 25, inclusive." + " You have 5 tries to guess my number!"); guesses = 5; max = 25; } // This else if statement confirms the difficulty level Hard, and gives the user instructions else if( userChoice == 3) { System.out.println("You have chosen level Hard. I will select a number between 1 and 100, inclusive." + " You have 8 tries to guess my number!"); guesses = 8; max = 100;```
And my purpose here is what I think you were asking me. So with this I thought that the method would interact with my max of 10, 25, and 100 and post the correct results.

I really thought I figured this out!
• May 23rd, 2013, 08:04 PM
Zaphod_b
Re: Creating a Secret Numbers game, need help with random number max and min.
Quote:

Originally Posted by kjdeutin
... Can you clarify the "What do you do to the result returned.." question?

What I meant was the following:

To get a number in the range 1, 2, ..., N, here's what you do:

• Call nextInt() with an argument that gives a value in the range 0, 1, 2, ..., N-1.

• Then add 1 to that value so that you will have a number in the range 1, 2, ..., N

• Taa-daa!

(Just as I did in the three examples that I showed.)

Cheers!

Z
• May 23rd, 2013, 09:16 PM
kjdeutin
Re: Creating a Secret Numbers game, need help with random number max and min.
I think I may just be confused as to where I put that. I'm sorry but I'm really bad with understanding just basic things right now. I think I understand what you're saying, I'm just having a hard time applying it. Will my changes be in here?

Code :

```public static int randInt(int max) { int min = 1;   java.util.Random rand = new java.util.Random(); return rand.nextInt(max - min) + (min + 1); }```
• May 23rd, 2013, 09:45 PM
Zaphod_b
Re: Creating a Secret Numbers game, need help with random number max and min.
Quote:

Originally Posted by kjdeutin
I think I may just be confused as to where I put that....

One more time:

You have a function whose argument is an int named max.
You want the function to return a number in the range 1, 2, ..., max.

My statements used N instead of max.

Write a Java statement or two that do what my statements do. Use max where I used N.

Get it? The function has code that uses nextInt() to calculate the return value. Can be a single statement or two statements. Could even be three statements.

Now, here's the thing: I think it's not a very good idea to create a new Random object every time you enter the function. I mean, that makes no sense at all to me. You want the function to use the random number generator, not to define a new one each time.

So...

One possibility is this: Make it some kind of static object that gets initialized once so that any function in your class can call nextInt() as many times as it wants to get as many random deviates as the program needs.

A skeleton of a test program:

Code java:

```import java.util.*;   public class Z {   public static Random rand = new Random();   public static void main(String [] args) {   // Call your function randInt() a bunch of times with an argument of 10 // and print the values.   } // End main   public static int randInt(int max) {   // Use rand.nextInt() to obtain a value in the range 1, 2, ..., max, // and return that value.   } // End randInt   } // End class definition```

Cheers!

Z
• May 23rd, 2013, 11:01 PM
kjdeutin
Re: Creating a Secret Numbers game, need help with random number max and min.
I am using
Code :

`randomNumber = randInt(max) + 1;`
as my "call" function I think.

--- Update ---

This is what I had originally:

Code :

``` public static int randInt(int max) {     java.util.Random rand = new java.util.Random(); return rand.nextInt(max); }```

and
Code :

`randomNumber = randInt(max) + 1;`