Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Delay Method and Random #?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Krocker




PostPosted: Fri Oct 14, 2011 11:14 am   Post subject: 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!
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri Oct 14, 2011 12:29 pm   Post subject: 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




PostPosted: Fri Oct 14, 2011 3:25 pm   Post subject: 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




PostPosted: Fri Oct 14, 2011 8:45 pm   Post subject: 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




PostPosted: Sat Oct 15, 2011 12:15 am   Post subject: 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




PostPosted: Sat Oct 15, 2011 7:59 am   Post subject: 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




PostPosted: Sat Oct 15, 2011 1:51 pm   Post subject: Re: RE:Delay Method and Random #?

Krocker @ Sat Oct 15, 2011 7:59 am wrote:
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)

Krocker @ Sat Oct 15, 2011 7:59 am wrote:

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()
Quote:

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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: