How to generate unique numbers using Fisher-Yates Algorithm with Java

Search for a command to run...

Hello
Beautiful content brother.โค๏ธโ๏ธ๐ฏ๐๐๐๐๐
Code is often read than written, making code readability a crucial aspect of software development. Readable code is easy to understand, allowing developers to follow the logic effortlessly through the lines. In this article, drawing from my experienc...

This article is a supplement to the recent project I built Colordash (formerly Guess the color game) , you can play the game here and read about its making here. edit: change game name to Color Dash RGB color space is a mathematical model used to rep...

A nextJS game with its objective of identifying as many colors as you can in a given amount of time using RGB color codes. In this article, I briefly describe how I built this game. Source code available on github. UI Design When I started working on...

Overview Privacy-friendly web analytics track a website's usage, gathering insightful data from visitors without collecting personal information. Goat Counter is my favorite tool that gets the job done. In this article, we will learn how to set it up...

If you have known beforehand that you want your react-app to be a PWA(Progressive Web App) you could have used the create-react template pwa like so : npx create-react-app my-app --template pwa But you didn't๐So here we are. In this article, I will...

In this article, we will be writing a java program that implements the paper and pencil method of the Fisher-Yates algorithm to generate nth unique numbers.
You can also use any list of numbers (or anything else it doesn't matter ) to shuffle their sequence.
But first ...
The Fisher-Yates shuffle is an algorithm named after Ronald Fisher and Frank Yates, and it is used to shuffle a sequence. Time Complexity: O(n). The main idea is that imagine you have ordered numbers written on a scratch paper, and you randomly strike out a number and write it down on another piece of paper. You do this until no unstruck number remains. The order in which the numbers are written is your shuffled sequence.
Here we go! ๐
throughout the program
int n = Integer.parseInt(args[0]) ; // amount of numbers to generate
int k; // random index of unstruckNums
ArrayList<Integer> unstruckNums = new ArrayList<Integer>();
ArrayList<Integer> results = new ArrayList<Integer>();
from 0 to n (exclusive)
for (int i = 0; i < n; i++) {
unstruckNums.add(i);
}
Now, what we want to do is strike out random numbers from the unstruckNums list and add them to a separate list which is the results. To achieve this we
which is between 0 and the amount of unstruckNums remaining
for (int i = 0; i < n; i++) {
// k represents the index of the number we want to strike out from the unstruckNums
k = (int) Math.floor(Math.random() * (unstruckNums.size()));
}
and strike it out from the unstruckNums list.
for (int i = 0; i < n; i++) {
k = (int) Math.floor(Math.random() * (unstruckNums.size()));
results.add(unstruckNums.get(k));
unstruckNums.remove(unstruckNums.get(k));
}
That's it, your done. Print out the results
System.out.println(results);
An example:
@tebza> javac UniqueNums.java
@tebza> java UniqueNums.java 12
[6, 1, 0, 5, 4, 10, 2, 11, 3, 8, 9, 7]
Here is the full code:
import java.util.ArrayList;
public class UniqueNums{
public static void main(String[] args) {
int n = Integer.parseInt(args[0]) ; // amount of numbers to generate
int k; // random index of unstruckNums
ArrayList<Integer> unstruckNums = new ArrayList<Integer>();
ArrayList<Integer> results = new ArrayList<Integer>();
// Fill the UnstruckNums list with numbers from 0 to n
for (int i = 0; i < n; i++) {
unstruckNums.add(i);
}
for (int i = 0; i < n; i++) {
// k represents the index of the number we want to strike out from the unstruckNums
k = (int) Math.floor(Math.random() * (unstruckNums.size()));
results.add(unstruckNums.get(k));
unstruckNums.remove(unstruckNums.get(k));
}
System.out.println(results);
}
}
The fisher-yates shuffle is a simple algorithm used to shuffle the sequence of lists. We have used it to shuffle an ordered list of numbers, to generate a list of unique numbers.