# Thread: help with math class and random numbers

1. Member Join Date
Sep 2011
Posts
4
Rep Power
0

## help with math class and random numbers

Java's Math class contains a static method called random.

The RootTwo class below computes an approximation to the the square root of 2. However, there's a missing statement in the code.

In the answer box provided, assign an appropriate random value to the variable val so that the code will calculate the intended approximation.

Hint: the algorithm works this way: the square root of 2 falls between 1 and 2. If you choose 1000 random non-integral values between 1 and 2, the fraction of these values that fall below the square root of 2 gives a reasonable estimate for the decimal part of that square root. (For example, if 951 of the 1000 fall below the square root of 2, then our estimate would be 1.951). It's easy to check if a number falls below the square root of 2: just multiply it by itself (square it), and compare the result with 2.0.

Java Code:
```import java.util.Scanner;

public class RootTwo {

public static void main(String[] args) {
System.out.println("Enter the number of iterations:");
Scanner scan = new Scanner(System.in);
int ct = 0;
int n = scan.nextInt();
double val;
for(int j = 0; j < n; j++){

return (1 + (int)(Math.random()*2));
^ cannot return a value from method whose result type is void

if (val * val < 2)
ct++;
}
System.out.println("With " + n + " iterations, my approximation to ");
System.out.println("the square root of 2 is " + (1 + (double)ct/n));
}
}```
Last edited by pbrockway2; 10-01-2011 at 02:20 AM. Reason: code tags added  Reply With Quote

2. Member Join Date
Sep 2011
Posts
4
Rep Power
0

## Re: help with math class and random numbers

i filled in the "return (1 + (int)(Math.random()*2));" as a guess. idk what im supposed to put in that line to make it work :/ and the computer made the cannot return value mark. im supposed to put something there to appease it  Reply With Quote

3. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: help with math class and random numbers

When posting code, please use the code tags. You put [code] at the start of the code and [/code] at the end. This means the code will be formatted correctly. There is a "#" button in the message composition pane that will add the tags around selected text.

-----

main() - as you have declared it - is a void method. That is, the method does not return a value. Hence the compiler tells you that there is a problem when you say "return (1+ etc".

What did you intend that line to do?  Reply With Quote

4. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: help with math class and random numbers

Our posts crossed!

OK, guessing won't work. Well, it might on occasion, but, in general, computer code is not created by guesswork.

So, do you understand the process that the question outlined for finding the square root of 2? In particular do you know what the role of the variables n, ct and val are?

val, in particular, is interesting. Not only what its value supposed to be, but also it should be declared and given that value somewhere.  Reply With Quote

5. Member Join Date
Sep 2011
Posts
4
Rep Power
0

## Re: help with math class and random numbers

this is just a homework question im not sure how to do and im a huge novice. on the website it gives the directions above the code and the box theyre referring to is where i put my line the computer said wasn't right. i need to put something to make random numbers between 1 and 2 i think to approximate the square root of 2  Reply With Quote

6. Member Join Date
Sep 2011
Posts
4
Rep Power
0

## Re: help with math class and random numbers

i undertand what n, ct, and val are. i just dont know how to represent all random numbers between 1 and 2 to approximate the square root of 2  Reply With Quote

7. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: help with math class and random numbers

i undertand what n, ct, and val are
Good. So declare val and assign it the value you understand it should have.  Reply With Quote

8. Member Join Date
Sep 2011
Posts
14
Rep Power
0

## Re: help with math class and random numbers

Return can only be used with non void methods, so either you rewrite your code so it can print that or you make a method (not a void) outside your main method and you will return your value to the main method.

I had an assigment that looked like this, only without math class and the number of iterations wasn't important.  Reply With Quote

9. Member Join Date
Aug 2011
Posts
16
Rep Power
0

## Re: help with math class and random numbers

Hey I'm not that good, but these might solve some of your problems!

Make your RootTwo look like this, it's a method which means you can call using a main method but it cannot be run by itself:
import java.util.Scanner;

public class RootTwo {

public int RootOfTwo(int iterations) { //The int here states that you'll be returning an integer to the caller
int ct = 0;
double val;
for(int j = 0; j < n; j++){

return (1 + (int)(Math.random()*2));

if (val * val < 2)
ct++;
}

}
}
Make a new class with the main method and put all these inside:
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println("Enter the number of iterations:");
System.out.println("With " + n + " iterations, my approximation to ");
System.out.println("the square root of 2 is " + (1 + (double)ct/n));
Finally a way to call the method with your main,
RootTwo(n);  Reply With Quote

10. Member Join Date
Sep 2011
Posts
14
Rep Power
0

## Re: help with math class and random numbers

Can you declare a double inside an int method?

Moreover what you will return is an int, but the square root of 2 is not an integer.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•