# Use a snippet ?

• 09-23-2013, 06:00 PM
huuduy216
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
• 09-23-2013, 06:10 PM
jim829
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 b-a?

Regards,
Jim
• 09-23-2013, 06:22 PM
JosAH
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
• 09-23-2013, 06:25 PM
jim829
Re: Use a snippet ?
I completely missed that.

Regards,
Jim
• 09-23-2013, 06:50 PM
huuduy216
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