Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Noob computer science student.

john larry
Greenhorn
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
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
Posts: 65218
95
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.

john larry
Greenhorn
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
Posts: 50196
79
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
Posts: 50196
79
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
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
Posts: 24212
35
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
Posts: 21498
84
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

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
Posts: 50196
79
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
i figured it out.

using this works:

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
Originally posted by john larry:
i figured it out.

using this works:

Where'd that "+ 1" come from?