Java Random Number

Associate
Joined
18 Mar 2007
Posts
291
Hi guys,

Being really thick here...

I'm trying to generate a random number between 0.3 and 1.

I have written the following basic code, but can't work out why it's not working (It's returning numbers <=0.3)!

Code:
public class Random
{

    private double x;

    public Random()
    {
        for(int i=0; i<10; i++){
            x=getRandomNumber();
            System.out.println(x);
        }
    }
    
    public double getRandomNumber()
    {
        double a;
        a=(Math.random());
        if(a<=0.3) {
            getRandomNumber();
        }
        return a;
    }
}

Any help appreciated, cheers.
 
ah, thanks a lot mate, knew it was something so silly! just couldn't get my head around it. diagram was a massive help, thanks a lot for taking the time to do that.
 
Back
Top Bottom