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

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.

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?

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.

Re: Help with simple math game coding

heres an 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

it return the same 2 numbers

http://s13.postimage.org/e1ytpqmw3/sim.jpg

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);

Re: Help with simple math game coding

It doesn't on my system. What OS and version of java are you using?

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

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);
}
}

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

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.

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?

Re: Help with simple math game coding

Defining it outside of any methods will make it a class variable.

Re: Help with simple math game coding

Re: Help with simple math game coding

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

Re: Help with simple math game coding

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?

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.

Re: Help with simple math game coding