
Use a snippet ?
For my homework, my teacher has provided us with the following code to generate a random number in the range a,b.
Code:
private static Random r = new Random ();
/**
* Returns a random number in the range [a, b].
*/
public static double
boundedRandomNumber (double a,double b) {
if(a >= b) {
System .err. println (" ERROR : empty interval .");
return 0.0;
}
double x = r. nextDouble ();
double range = Math . abs (a  b);
return x * range + a;
}
my question is How can I use this piece of code in my code ? :? Im new to Java and programming, sorry for my ignorance

Re: Use a snippet ?
Well, it depends on what your program needs to do. But I have a different question. If the teacher checks for an empty interval, why does s/he use the absolute value method? Why not just calculate range as ba?
Regards,
Jim

Re: Use a snippet ?
If the type of the parameters is supposed to be double, the code should say so; Java needs the type of everything, including parameters; now the (lousy) code doesn't even compile.
kind regards,
Jos

Re: Use a snippet ?
I completely missed that.
Regards,
Jim

2 Attachment(s)
Re: Use a snippet ?
This is the homework that I'm working on. I find the method a little bit weird though. I mean the range is very large, chances that none of the points are in the curve is pretty big.
Attachment 5598
About the code, I might have mistyped it. so here is a screenshot of it.
Attachment 5599
Please help :(