Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RandomNumbers {
- public static void main(String[] args) {
- // Random double value between 0 and 1
- System.out.println(Math.random());
- // Random double value between 0 and 9
- double a = Math.random() * 10; // by default is of double type
- System.out.println(a);
- // Random int value between 0 and 9
- int b = (int)(Math.random() * 10); // to explicitly change to int type
- System.out.println(b);
- // Random int value between 1 and 10
- int c = (int)(Math.random() * 10) + 1; // place +1 outside the parentheses
- System.out.println(c); // if place inside parentheses, we would get a random int between 0 and 10
- // Random int value between 10 and 20
- // (Math.random() * (maxValue - minValue + 1)) + minValue.
- int d = (int)(Math.random() * 11 ) + 10; // +10 meaning to start at 10
- System.out.println(d);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement