Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

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

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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?

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Creating a Secret Numbers game, need help with random number max and min.

    Quote Originally Posted by kjdeutin View Post
    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

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Secret Numbers game, need help with random number max and min.

    Quote Originally Posted by Zaphod_b View Post
    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:

    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:

    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!

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Creating a Secret Numbers game, need help with random number max and min.

    Quote Originally Posted by kjdeutin View Post
    ... 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

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    public static int randInt(int max)
    	{		
    		int min = 1;
     
    			java.util.Random rand = new java.util.Random();
    			return rand.nextInt(max - min) + (min + 1);
    	}

  8. #8
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Creating a Secret Numbers game, need help with random number max and min.

    Quote Originally Posted by kjdeutin View Post
    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:

    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

  9. #9
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Secret Numbers game, need help with random number max and min.

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

    --- Update ---

    This is what I had originally:

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

    and
    randomNumber = randInt(max) + 1;

Similar Threads

  1. Creating Random unused ID number for every library member.
    By clarky2006 in forum Java Theory & Questions
    Replies: 95
    Last Post: March 20th, 2013, 06:33 AM
  2. Replies: 5
    Last Post: November 29th, 2012, 12:25 PM
  3. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 07:50 PM
  4. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  5. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 07:24 PM