Computing.Net > Forums > Programming > random number in java

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

random number in java

Reply to Message Icon

Name: nunyabiznaz
Date: September 14, 2004 at 11:38:22 Pacific
OS: Windows ME
CPU/Ram: Pentium II/350 MHz
Comment:

i have a simple game where the user guesses a number which is randomly generated and then they see how many guesses it takes to get the number, but once they get it the first time and start playing again and again the random number stays the same, it doesn't generate a new one. what to do? thanks.

public void generateMyNumber()
{
int aNumber = 0;
aNumber = (int)((Math.random() * 10));
setMyNumber(aNumber);
}

public void playTheGame()
{
generateMyNumber();



Sponsored Link
Ads by Google

Response Number 1
Name: jro
Date: September 14, 2004 at 11:54:09 Pacific
Reply:

Java isn't my expert field, however I did take it in college a few years back

you need to call the Function generateMyNumber each time you want to play the game

I see you Zero'd out the variable aNumber within the function, that should have done it.

you could also try setting the Function to a Variable, such as int number=0;
number=generateMyNumber();

I haven't done this stuff in years, but I would give that a try

I'm sorry If I wasn't helpful

Good luck

jro



0

Response Number 2
Name: BlueRaja
Date: September 14, 2004 at 13:42:19 Pacific
Reply:

Ya, you're probably not resetting the number each time they want to play.

Also, how is the random number seeded? (Never used the random number function in java, so the user id est you might not be able to seed it...)

AKhalifman@hotmail.com


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


c++ singleton Extract comp. alias in a ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: random number in java

Random Numbers in Java www.computing.net/answers/programming/random-numbers-in-java/8564.html

Find prime numbers in Java? www.computing.net/answers/programming/find-prime-numbers-in-java/11287.html

Random numbers in ASSEMBLY www.computing.net/answers/programming/random-numbers-in-assembly/9466.html