Java Task 1 Life is Random This is a simple program that di
Java
Task #1 – Life is Random This is a simple program that displays random numbers that use the Java Random Number Object. No user input is required for this program. For your output: First, display 10 random (non-Integer) numbers between 0 and 1. Second, display 10 random (Integer) numbers between 0 and 100. Display both of these in the format:
Random Numbers between 0 and 1
Random #1 : 0.4323423
Random #2 : 0.7785267
Note: Be accurate in any statements that you display to the user (i.e., spacing, separate lines when necessary, headings, clear statements as to what the user is to do or enter. Points will be taken off for a sloppy user interface!
Solution
RandmNum.java
import java.util.Random;
public class RandmNum {
public static void main(String[] args) {
Random r = new Random();
System.out.println(\"Random Numbers between 0 and 1: \");
for(int i=1; i<=10; i++){
float randNum = r.nextFloat() * 1.0f;
System.out.println(\"Random #\"+i+\" : \"+randNum);
}
System.out.println(\"Random Numbers between 0 and 100: \");
for(int i=1; i<=10; i++){
int randNum = r.nextInt(101);
System.out.println(\"Random #\"+i+\" : \"+randNum);
}
}
}
Output:
Random Numbers between 0 and 1:
Random #1 : 0.019478023
Random #2 : 0.23218358
Random #3 : 0.0976426
Random #4 : 0.87054664
Random #5 : 0.45886773
Random #6 : 0.3475325
Random #7 : 0.035101652
Random #8 : 0.9892369
Random #9 : 0.068131745
Random #10 : 0.6682492
Random Numbers between 0 and 100:
Random #1 : 26
Random #2 : 98
Random #3 : 25
Random #4 : 20
Random #5 : 86
Random #6 : 91
Random #7 : 34
Random #8 : 75
Random #9 : 85
Random #10 : 33

