Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Noob computer science student.

 
john larry
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
john larry
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49442
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49442
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
john larry
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 49442
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i figured it out.

using this works:

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by john larry:
i figured it out.

using this works:



Where'd that "+ 1" come from?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic