
-----------------------------------
Krocker
Fri Oct 14, 2011 11:14 am

Delay Method and Random #?
-----------------------------------
hiya, ok so im new to this and im learning java at school. however, my teacher doe snot know much of java and its a pain learning from him, there for i wanted to learn a head. im trying to create a simple random number generator, but im not sure how to make the random number and how to delay the loops. some one plz help me!

-----------------------------------
Insectoid
Fri Oct 14, 2011 12:29 pm

RE:Delay Method and Random #?
-----------------------------------
Why would you want to delay the loops? Random number generators are a LOT harder to make than one might think. Good thing Java's got a Math.Rand() function (or something similar).

-----------------------------------
Krocker
Fri Oct 14, 2011 3:25 pm

RE:Delay Method and Random #?
-----------------------------------
b/c i wanted to loop the program where it randomnizes  a int number and the asks the user to guess the number, i wanted the delay to delay the loop after it displays the resualt so that it does not clear in a second and continue. plz help. im so confused.

-----------------------------------
RandomLetters
Fri Oct 14, 2011 8:45 pm

RE:Delay Method and Random #?
-----------------------------------
Thread.sleep(milliseconds);

But instead of just clearing it after a set time, it might be more friendly to let the user tell you when he wants to try again.

-----------------------------------
Zren
Sat Oct 15, 2011 12:15 am

RE:Delay Method and Random #?
-----------------------------------
It also might be more fun to turn this into a game of higher/lower. Then you could count the guesses.

If you tackle that, then here's a puzzle for you. What's the maximum number of guesses needed to guess a random number between 1 and a hundred? For each wrong answer I'd tell you if the answer is higher or lower than your guess.

-----------------------------------
Krocker
Sat Oct 15, 2011 7:59 am

RE:Delay Method and Random #?
-----------------------------------
oh, i know this, i would do a if statment. the first one will be if the answer is right, if true then it will display a message, and will exit the loop, if incorrect the the system will add one to a varible for # of guesses and then go into another if to check if the answer was to high or to low and it will display the corresponding message. 


as for how many guesses b/t 1 to 100, i would say 100 guesses?


but i dont see how that will help me learn delays and random number generaters. also i was looking at Math.Random(), but i not sure how to use it.

-----------------------------------
Tony
Sat Oct 15, 2011 1:51 pm

Re: RE:Delay Method and Random #?
-----------------------------------
as for how many guesses b/t 1 to 100, i would say 100 guesses?
Too high. Which number would I have to guess for you to take 100 tries?

e.g. Guess "99". You are either:
 - right. Done in 1 move.
 - too low. Then you know the answer is 100. Done in 2 moves.
 - too high. Linearly try every number down. Done in 99 moves.

(though by the same approach "99" also too high)


but i dont see how that will help me learn delays and random number generaters. also i was looking at Math.Random(), but i not sure how to use it.
It's an important problem to be able to solve; basis of many algorithms.

Also, docs for Math.random() are pretty straight forward. http://download.oracle.com/javase/6/docs/api/java/lang/Math.html#random()

public static double random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

What are you not sure about?
