Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Generate Random Numbers in Java without Duplicates?

Generate Random Numbers in Java

Condition for Generating Random Numbers without Duplicates in Java

  • Description: Generating random numbers without duplicates in Java can be achieved by using a Set to store the numbers, ensuring that only unique values are kept. A HashSet is commonly used because it automatically handles duplicates by not allowing the same number to be added more than once. A Random object or ThreadLocalRandom can be used to generate random numbers within a specified range. The process involves generating random numbers and checking if they already exist in the set before adding them. If the number is not in the set, it is added; if it already exists, another number is generated. This continues until the desired number of unique random numbers is generated. This approach is efficient and guarantees that all numbers in the set will be unique.
Sample Source Code
  • # RandomNumber.java
    package JavaSamples;

    import java.util.HashSet;
    import java.util.Random;

    public class RandomNumber extends javax.swing.JFrame {

    public RandomNumber() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jLabel3 = new javax.swing.JLabel();
    jTextField2 = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Random Number Generator");

    jLabel2.setText("Count :");

    jTextField1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1ActionPerformed(evt);
    }
    });

    jButton1.setText("Generate");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    jLabel3.setText("Range :");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(89, 89, 89)
    .addComponent(jButton1))
    .addGroup(layout.createSequentialGroup()
    .addGap(52, 52, 52)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
    .addGroup(layout.createSequentialGroup()
    .addGap(20, 20, 20)
    .addComponent(jLabel2)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(48, 48, 48)
    .addComponent(jLabel3)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
    .addGap(88, 88, 88)
    .addComponent(jLabel1))))
    .addContainerGap(61, Short.MAX_VALUE))
Screenshots
  • STEP 1: The user creates the GUI for generating random numbers.
  • The user creates the GUI for generating random numbers.

  • STEP 2: The user specifies the count of random numbers and the range for generating them.
  • The user specifies the count and range.

  • STEP 3: The user clicks the "Generate" button, which generates the random numbers without duplicates.
  • The random numbers are generated.