Question: This is java, how the equation intNumber = rand.nextInt(n)-n/2 is used? I don't understand the concept? How the 1.0 in this equation affects the range

This is java, how the equation intNumber = rand.nextInt(n)-n/2 is used? I don't understand the concept? How the "1.0" in this equation affects the range doubleNumber = rand.nextDouble() + 1.0; The 0 and 1.0 is ranges for the double, why the range in the code for double is(-6,5)?

public static void main(String[] args) {

int intNumber;

double doubleNumber;

int n = 12;

for(int count = 0; count < 10; count++)

{

Random rand = new Random();

intNumber = rand.nextInt(n)-n/2;

//range [-6, 5]

System.out.println(count + "\t" + intNumber);

}

}

}

I think this is the same thing,

int intNumber;

double doubleNumber;

int n = 12;

for(int count = 0; count < 10; count++)

{

Random rand = new Random();

//Range [0.0+1.0, 1.0 + 1.0); i.e., [1.0, 2.0).

doubleNumber = rand.nextDouble() + 1.0;

System.out.println(count + "\t" + doubleNumber);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!