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 18 of 18

Thread: Help with simple math game coding

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

    Question Help with simple math game coding

    hi java programmers,

    i have a simple question about my math game,
    i need to randomize 2 numbers using 1 method

    this is the method i have to randomize the number from 0-20

    public static int addsubrandomizer ()
        {
            Random rand = new Random ();
     
            int ranaddsub = rand.nextInt (21);
            return ranaddsub;
        }

    and here is where i return my data to

    addsubnumber1 = addsubrandomizer ();
    addsubnumber2 = addsubrandomizer ();

    but i cannot figure out why they output the same number for addsumnumber1 and addsumnumber2

    can someone help me figure this out please

    thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,614
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Help with simple math game coding

    What number do they return? It is possible for a random number generate the same number more than once.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    jmd (February 26th, 2013)

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

    Default Re: Help with simple math game coding

    they return the same set of numbers for both addsumnumber1 and addsumnumber2, how can i make the method to return 2 different value for each?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,614
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Help with simple math game coding

    Can you write a small simple program that compiles, executes and shows the problem?
    Execute the code, copy the console contents that show the problem and paste it here.
    When I execute the code I get different numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    jmd (February 26th, 2013)

  7. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Help with simple math game coding

    I'd recommend just using the math library approach
    import java.lang.Math;
     
    addsubnumber1 = (int) Math.floor(Math.random()*21);
    addsubnumber2 = (int) Math.floor(Math.random()*21);

  8. The Following User Says Thank You to JakeM1130 For This Useful Post:

    jmd (February 26th, 2013)

  9. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,614
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Help with simple math game coding

    It doesn't on my system. What OS and version of java are you using?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Help with simple math game coding

    i posted a reply with sample and ss but need mod confirm

    not sure why but i'm using Ready to Program v1.7.1

    Quote Originally Posted by JakeM1130 View Post
    I'd recommend just using the math library approach
    import java.lang.Math;
     
    addsubnumber1 = (int) Math.floor(Math.random()*21);
    addsubnumber2 = (int) Math.floor(Math.random()*21);
    i will give this a try thanks

  11. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Help with simple math game coding

    I'm on Vista and Java 1.6. Note that's not a full code though. In full, it would look something like
    import java.awt.*;
    import java.lang.Math;
     
    public class RandomNumberGenerator {
    	public static void main(String []args) throws AWTException{
    		int addsubnumber1 = (int) Math.floor(Math.random()*21);
    		int addsubnumber2 = (int) Math.floor(Math.random()*21);
    		System.out.println(addsubnumber1);
    		System.out.println(addsubnumber2);
    	}
    }

  12. The Following User Says Thank You to JakeM1130 For This Useful Post:

    jmd (February 28th, 2013)

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

    Default Re: Help with simple math game coding

    here was my original example code

    import java.util.Random;
     
    // The "SimpleRandom" class.
    public class SimpleRandom
    {
     
    public static void main (String[] args)
        {
            int addsubnumber1 = addsubrandomizer ();
            int addsubnumber2 = addsubrandomizer ();
     
            System.out.println (addsubnumber1);
            System.out.println (addsubnumber2);
        } // main method
     
     
        public static int addsubrandomizer ()
        {
            Random rand = new Random ();
            int ranaddsub = rand.nextInt (21);
            return ranaddsub;
        }
    } // SimpleRandom class

    and i got both number as same

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,614
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Help with simple math game coding

    How many times did you execute it? I executed it about 10 times. Sometimes the numbers were the same and sometimes they were different.

    Try defining the Random class object outside of the method as a class variable.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Help with simple math game coding

    i executed many times and all of them came back the same

    how would i define the Random class object outside of the method as a class variable?

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,614
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Help with simple math game coding

    Defining it outside of any methods will make it a class variable.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    jmd (February 28th, 2013)

  18. #13
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Help with simple math game coding

    G

  19. The Following User Says Thank You to javaiscool For This Useful Post:

    jmd (February 28th, 2013)

  20. #14
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Help with simple math game coding

    i did the math library approach since its more efficient thx though

  21. #15
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Help with simple math game coding

    H

  22. The Following User Says Thank You to javaiscool For This Useful Post:

    jmd (February 28th, 2013)

  23. #16
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Help with simple math game coding

    Quote Originally Posted by javaiscool View Post
    Are you using .floor and .ceil to lower/raise each random double then type-casting to int, to ensure two different results? If so, creative. Well done!
    yes i used the math.floor as jake recommended and it worked great

    i have a question about checking the inputted from user
    i want it to check if the user input a integer set in a range, and not allow them to input characters, symbols or words,
    how could i go about doing this?

  24. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,614
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Help with simple math game coding

    not allow them to input characters, symbols or words,
    You can't stop the user from entering anything he wants to enter.
    You can refuse to accept input that is not valid.
    That implies having a loop that doesn't exit until the program gets the desired input.
    Use a while loop that doesn't exit until the program says its ok to exit.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #18
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Help with simple math game coding

    D

Similar Threads

  1. I need help making a simple math game in java.?
    By bnichs5 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 3rd, 2013, 06:14 AM
  2. Can someone help me(Simple coding)
    By poldz123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 18th, 2012, 10:14 PM
  3. Please i need help in a simple coding
    By ookhai in forum Object Oriented Programming
    Replies: 13
    Last Post: July 6th, 2012, 10:19 AM
  4. Need help with simple Shopping cart math
    By I hate Java! in forum Loops & Control Statements
    Replies: 3
    Last Post: November 6th, 2011, 09:46 PM
  5. i need coding for simple login
    By nag in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: September 29th, 2011, 02:30 PM

Tags for this Thread