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
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
Use a loop to keep generating random ints until randomOne != randomTwo.
Quote:
Originally Posted by
jmd
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?
By defining it outside the method. Literally place the statement creating the random object outside of the method, but within the class.
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
Quote:
Originally Posted by
jmd
i did the math library approach since its more efficient thx though
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!
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
Quote:
Originally Posted by
jmd
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?
@JMD
Hey, I know what you mean; I use this all the time to demand the user enter an integer:
Code :
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
while (!input.hasNextInt()) // while the next token in the buffer is not an integer
{
input.next(); // advance to next token
System.out.println("That is not an integer; try again."); // deliver error message
}
int x = input.nextInt();
// after loop exits, assigns the int to var x (or replace this with whatever you want to do with the int)
A vague concept of what's happening is:
program enters the while loop, checks the scanner buffer to see if the next token is an int; if not, input.next(); moves to the next token (separated by the delimiter, whitespace), and checks for an int. The program will prompt the user to input something within that loop as well. After an int is found, the program exits the while loop, and the assigns the int to variable x, as it is the nextInt(); in the buffer, i.e., the next token in the scanner buffer is an int.
That's the best way I can explain it with my knowledge, and may not be entirely accurate description. But, it works.
Norm is right to say that you can't prevent the user from entering other datatypes; you weren't being very precise with your word choice, however, I know what you meant.