Random Ranges and Shuffling
Bounded randoms and collections shuffle.
Random Ranges and Shuffling is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Ranges and Shuffling
Real programs rarely want 0 to bound-1. This lesson covers arbitrary ranges, randomly shuffling collections, and picking random elements.
A Custom Range Formula
To produce an int from min to max inclusive, use min + rng.nextInt(max - min + 1).
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rng = new Random();
int min = 10, max = 20;
int value = min + rng.nextInt(max - min + 1);
System.out.println("10 to 20: " + value);
}
}nextInt with Origin and Bound
Modern Java lets you call nextInt(origin, bound) directly: origin inclusive, bound exclusive. This avoids the manual formula.
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rng = new Random();
int value = rng.nextInt(10, 21);
System.out.println("10 to 20: " + value);
}
}Random Doubles in a Range
For a double from lo to hi, use lo + rng.nextDouble() * (hi - lo).
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rng = new Random();
double lo = 5.0, hi = 7.5;
double value = lo + rng.nextDouble() * (hi - lo);
System.out.println(value);
}
}Shuffling a List
Collections.shuffle(list) randomly reorders a List in place. Great for card decks and randomized quizzes.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> deck = new ArrayList<>(List.of(1, 2, 3, 4, 5));
Collections.shuffle(deck);
System.out.println(deck);
}
}Reproducible Shuffles
Pass a seeded Random as a second argument to get a deterministic shuffle: Collections.shuffle(list, new Random(seed)).
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
List<Integer> deck = new ArrayList<>(List.of(1, 2, 3, 4, 5));
Collections.shuffle(deck, new Random(99));
System.out.println(deck);
}
}Shuffle Needs a Mutable List
List.of(...) creates an immutable list. Shuffling it throws UnsupportedOperationException. Wrap it in new ArrayList<>(...) first.
Picking a Random Element
Choose a random element by generating a random index and using get.
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
List<String> colors = List.of("red", "green", "blue");
Random rng = new Random();
String pick = colors.get(rng.nextInt(colors.size()));
System.out.println("Chosen: " + pick);
}
}Shuffling an Array
Arrays have no built-in shuffle. The classic approach is the Fisher-Yates algorithm: swap each element with a random earlier-or-equal index.
import java.util.Arrays;
import java.util.Random;
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
Random rng = new Random(3);
for (int i = arr.length - 1; i > 0; i--) {
int j = rng.nextInt(i + 1);
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
System.out.println(Arrays.toString(arr));
}
}Random Sampling
To draw several distinct items, shuffle a copy and take the first few, or use a Set to track already-chosen indices. Shuffling then slicing is simplest for small lists.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> pool = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6, 7, 8));
Collections.shuffle(pool);
List<Integer> sample = pool.subList(0, 3);
System.out.println("Sample: " + sample);
}
}Practical Notes
Shuffle when you need a full random reordering; use a custom range formula when you need bounded values. Always remember the off-by-one when an upper bound should be inclusive.
Quick Check
Test your understanding of ranges and shuffling.
Recap
You learned ranges and shuffling.
- Use
min + nextInt(max - min + 1)ornextInt(origin, bound)for ranges. Collections.shufflereorders a mutable list in place.- Seed the shuffle for reproducibility.
- Pick random elements via a random index.
Frequently asked questions
Is the “Random Ranges and Shuffling” lesson free?
Yes — the full text of “Random Ranges and Shuffling” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Random Ranges and Shuffling”?
Bounded randoms and collections shuffle. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Random Ranges and Shuffling” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Math Class
- Generating Random Numbers
- Random Ranges and Shuffling
- Math Rounding and Limits