Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Wednesday, January 21, 2009

Generate Random Numbers

/**

* Generate random nos. If the total of these nos exceeds 20, terminate the

* program.

*

*

*/

public class RandomGenTest {

/**

* generate and display random values and make sum of them when sum reaches

* upto 20 stop the loop

*/

public static void generateRandomNumbers() {

try {

// store the sum and randvalue

double dblSum = 0, dblRandomValue = 0;

// loop till sum is less than 20

while (dblSum <= 20) {

dblRandomValue = Math.random();

dblSum += (dblRandomValue * 10);

System.out.println("Random Value : " + dblRandomValue);

}

// display the sum value

System.out.println("Sum Of Random Values : " + dblSum);

} catch (Exception e) {

System.out.println("Error Ocurred!!");

e.printStackTrace();

}

}

/**

* execution begin from here

*

* @param args

* commandline arguments

*/

public static void main(String[] args) {

generateRandomNumbers();

}

}

No comments:

Post a Comment

Followers