Help with simple math game coding

• February 26th, 2013, 04:59 PM
jmd
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

Code :

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

and here is where i return my data to

Code :

```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
• February 26th, 2013, 05:18 PM
Norm
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.
• February 26th, 2013, 06:21 PM
jmd
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?
• February 26th, 2013, 06:32 PM
Norm
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.
• February 26th, 2013, 07:14 PM
JakeM1130
Re: Help with simple math game coding
I'd recommend just using the math library approach
Code :

```import java.lang.Math;   addsubnumber1 = (int) Math.floor(Math.random()*21); addsubnumber2 = (int) Math.floor(Math.random()*21);```
• February 26th, 2013, 07:31 PM
Norm
Re: Help with simple math game coding
It doesn't on my system. What OS and version of java are you using?
• February 26th, 2013, 07:36 PM
jmd
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
I'd recommend just using the math library approach
Code :

```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
• February 26th, 2013, 07:39 PM
JakeM1130
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
Code :

```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); } }```
• February 26th, 2013, 07:47 PM
jmd
Re: Help with simple math game coding
here was my original example code

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
• February 26th, 2013, 07:53 PM
Norm
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.
• February 26th, 2013, 07:58 PM
jmd
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?
• February 26th, 2013, 08:25 PM
Norm
Re: Help with simple math game coding
Defining it outside of any methods will make it a class variable.
• February 27th, 2013, 02:09 AM
javaiscool
Re: Help with simple math game coding
G
• February 27th, 2013, 09:23 AM
jmd
Re: Help with simple math game coding
i did the math library approach since its more efficient thx though
• February 27th, 2013, 04:24 PM
javaiscool
Re: Help with simple math game coding
H
• February 28th, 2013, 04:55 PM
jmd
Re: Help with simple math game coding
Quote:

Originally Posted by javaiscool
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?
• February 28th, 2013, 06:08 PM
Norm
Re: Help with simple math game coding
Quote:

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.
• February 28th, 2013, 09:49 PM
javaiscool
Re: Help with simple math game coding
D