Skip to main content

Chapter 5a - Random

Random Introduction​


import java.util.Random;
class Main {
public static void main(String args[]) {

Random rand = new Random(); //creates object of class Random which is used to generate random number
int randomNum = rand.nextInt(4); //generates random numbers
System.out.println(randomNum);
}
}

πŸ§ͺ Try the code out~!

πŸ‘€

Coin Simulation​

import java.util.Scanner;
import java.util.Random;
class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
Random rand = new Random(); //creates object of class Random which is used to generate random number
System.out.println("This is a simulation of a coin toss.");
System.out.println("Tossing coin now...\n");
int randomNum = rand.nextInt(2); //generates random numbers 0 & 1
if (randomNum == 0) {
System.out.println("HEADS...\n");
} else {
System.out.println("TAILS...\n");
}
}
}

πŸ§ͺ Try the code out~!

(i) Does the user provide any input here?


(ii) What are the possible outputs?


(iii) Which line of code requires that the random module be imported?


(iv) What are the values stored by randomNum ?


(v) What module is required to generate random numbers?


Exercise: Team Selection​

tip
  • Create a program to randomnly assign someone in either blue or red team

πŸ™‹β€β™€οΈ expected

Introduce Random Module - Formatting​

import java.math.RoundingMode;
import java.text.DecimalFormat;
class Main {
public static void main(String[] args) {
for(int count = 0; count< 5; count++){
System.out.println(Math.random());
}
}
}

πŸ§ͺ Try the code out~!

Formatting Random​

import java.math.RoundingMode;
import java.text.DecimalFormat;
class Main {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("0.00");
for(int count = 0; count< 5; count++){
double d=Math.random();
System.out.println(df.format(d));
}
}
}

πŸ§ͺ Try the code out~!


Mini quiz β€” Random ranges​

Why we cannot autograde a random value

Random is unpredictable on purpose, so the challenges below test the decision logic around a random result, not the random number itself β€” that is the part you actually write.

Quiz

Random ranges

1 / 2

rand.nextInt(6) can return which set of values?

Pick an answer to see if you are right.


Challenge: describe a die roll​

You will not generate the random number here β€” instead write the decision logic a die-rolling program needs. Write describeRoll(int roll) where roll is already a value from 1 to 6: return "Lucky seven? No, but nice roll!" is wrong β€” instead return "You rolled a " + roll. Then in a real program you would call this with rand.nextInt(6) + 1.


Challenge: format to two decimals​

Read a double. Print it formatted to exactly two decimal places using DecimalFormat (the same tool the "Formatting Random" example used).


Chapter summary​

Key takeaways
  1. new Random() creates a random number generator; rand.nextInt(bound) gives a value from 0 up to (not including) bound.
  2. Add 1 to nextInt(n) to shift a 0..n-1 range into a 1..n range (a classic die roll).
  3. Since randomness cannot be tested by comparing exact output, separate the decision logic (what to do with a roll) from the random source (how the roll was generated) β€” you can test the logic on its own by passing in a known value.
  4. DecimalFormat("0.00") formats a double to a fixed number of decimal places, useful for cleaning up Math.random() output.

Exercise​

Activity: Coin toss message

Write coinMessage(int flip) where flip is 0 or 1 (as if it came from rand.nextInt(2)). Return "HEADS" for 0, "TAILS" for 1.