wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Noob computer science student. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Noob computer science student." Watch "Noob computer science student." New topic
Author

Noob computer science student.

john larry
Greenhorn

Joined: Nov 23, 2008
Posts: 9
Can someone tell me, i'm trying to make the program choose a random number between 0 and 99 and the only way i know to call a random number is Math.random(); and that is for doubles between 0.0 and 1.0, so i'm not sure what to do. Help please?
david john
Greenhorn

Joined: Oct 16, 2008
Posts: 16
You need to cast it to get the int values. For example

[ November 23, 2008: Message edited by: david john ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

david john, please use real words when posting to the forums. Abbreviations such as "U" in place of "you" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
john larry
Greenhorn

Joined: Nov 23, 2008
Posts: 9
okay, so now i'm having another problem, i want the program to loop back to beginning if it doesn't guess the correct number. which would use a while statement right? i don't know what to put in the parenthesis for the while statement though..This is what i have:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39092
    
  23
Welcome to JavaRanch

Have you found the Random class, which has methods to supply (pseudo-)random integer numbers in a particular range?

Beware: if you want numbers between 0 and 99 you have to pass 100 as an argument.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39092
    
  23
We prefer you to find out the solutions for yourself; you will find details about the while loop here, and you should find what they mean by "expression" here (I hope!)
john larry
Greenhorn

Joined: Nov 23, 2008
Posts: 9
I'm doing this:



but i'm sure how to make the random number between a certain range, i can make it like between 0 and 100 or 0 and 50 or whatever, but what about like between 100 and 200, etc?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Let's say you know how to get random numbers between 0 and 100. What mathematical transformation could you apply to give you random numbers between 50 and 150? (Hint: you learned to do it when you were 5).


[Jess in Action][AskingGoodQuestions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

but i'm sure how to make the random number between a certain range, i can make it like between 0 and 100 or 0 and 50 or whatever, but what about like between 100 and 200, etc?


Think about it a bit.... If you have number between 0 and 100, how do you convert this number to a number between 100 and 200?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
john larry
Greenhorn

Joined: Nov 23, 2008
Posts: 9


I'm thinking this works, but it doesn't seem to work for larger numbers..

When i'm making 150 the x and 100 the y it wants to guess 200 and other numbers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39092
    
  23
I would have added a boolean guessedRight variable; you can alter that inside the loop, and use it to terminate the loop if your user guesses right.
john larry
Greenhorn

Joined: Nov 23, 2008
Posts: 9
i figured it out.

using this works:

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by john larry:
i figured it out.

using this works:



Where'd that "+ 1" come from?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Noob computer science student.