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β
- 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β
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.
Random ranges
1 / 2rand.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β
new Random()creates a random number generator;rand.nextInt(bound)gives a value from0up to (not including)bound.- Add 1 to
nextInt(n)to shift a0..n-1range into a1..nrange (a classic die roll). - 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.
DecimalFormat("0.00")formats adoubleto a fixed number of decimal places, useful for cleaning upMath.random()output.
Exerciseβ
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.