Generate Unique Random Numbers Without Repeating In Java

Generate Unique Random Numbers Without Repeating In Java

Most of the programs need random functions for which we need generation of random number...
But repeating of same number again and again fails our program.
For Solving this problem you can use the following codes:

Java Program on Random Numbers Without Repeating :

import java.io.*;
import java.util.Random;
public class RandomNoRep{
    
public static String store="";
public static int num=1,focus=0,checkRep=0;
public static String[] storeArray;

public static class NoRepNum{
//Random Number Method
int random(int a){
    Random rand = new Random();
    int prob = (int) (a * rand.nextDouble()+1);
    return prob;
}
//NoRep Method
public int NoRep(int a){
    int Rno=0;
    for(int i=0;i<100 data-blogger-escaped-------="" data-blogger-escaped-arg="" data-blogger-escaped-break="" data-blogger-escaped-bufferedreader="" data-blogger-escaped-bufread="new" data-blogger-escaped-check.equals="" data-blogger-escaped-check="storeArray[j];" data-blogger-escaped-checkrep="0;" data-blogger-escaped-focus="1;" data-blogger-escaped-for="" data-blogger-escaped-i="" data-blogger-escaped-if="" data-blogger-escaped-inputstreamreader="" data-blogger-escaped-int="" data-blogger-escaped-istream="" data-blogger-escaped-j="1;j" data-blogger-escaped-len="" data-blogger-escaped-main="" data-blogger-escaped-norepnum="" data-blogger-escaped-num="" data-blogger-escaped-numbers="" data-blogger-escaped-public="" data-blogger-escaped-random="" data-blogger-escaped-range="0;" data-blogger-escaped-repetation="" data-blogger-escaped-return="" data-blogger-escaped-rno="" data-blogger-escaped-static="" data-blogger-escaped-store="store+num+" data-blogger-escaped-storearray="store.split(" data-blogger-escaped-string="" data-blogger-escaped-system.out.println="" data-blogger-escaped-tring="" data-blogger-escaped-var="new" data-blogger-escaped-void="" data-blogger-escaped-without="" data-blogger-escaped-ystem.in="">=0;j++){
    try{
        System.out.print("\nEnter Range (How Many Numbers You Want?) : ");
        range=Integer.parseInt( bufRead.readLine() ) ;
        break;
    } catch(Exception Number){
        System.out.println("\n***** Wrong Entry *****\n");
        continue;
    }
    }
    System.out.println("\n++++ Generating Numbers ++++\n");
    for(int i=1;i<=range;i++){
        System.out.print( var.NoRep( range ) + " , " );
    }
    System.out.println("\n\n © codenirvana.blogspot.com ");
}
}

Output of the above program :

The above program used the NoRepNum class which is the main part of the code. If you have any problem or don't know how to use the above program's code! Here's the method to do it :


public String store="";
public int num=1,focus=0,checkRep=0;

public class NoRepNum{

//Random Number Method
int random(int a){
    Random rand = new Random();
    int prob = (int) (a * rand.nextDouble()+1);
    return prob;
}
//NoRep Method
public int NoRep(int a){
    int Rno=0;
    for(int i=0;i<100;i++){
     num=random(a);
     int len=0;
     String[] storeArray;
     storeArray=store.split(",");
     len=storeArray.length;
     if(focus==0){
         len=0;
         focus=1;
     }
     for(int j=0;j<len;j++){
         String check=storeArray[j];
         if(check.equals(""+num)){
            checkRep=1;
         }
     }
    if(checkRep==0){
        store=store+num+",";
        Rno=num;
        break;
    }
    checkRep=0;
}
    return Rno;
}
}



//**********************************HOW-TO-USE***********************//

NoRepNum <objectName> = new NoRepNum();
<whereToShow>.setText(""+<objectName>.NoRep( <value>) );

here, <objectName>.NoRep( <value> ) means

for obtaining digits from :
0 to 10 value = 10
0 to 100 value = 100
and so on......

NOTE: Always 0 comes in the end (last digit of the process). 
     if you don't want so, do following changes in 'random()' method :

int random(int a){
    Random rand = new Random();
    int prob = (int) (a * rand.nextDouble());
    return prob;
}

              but still the process ends with 0.

Download my random number project -  click here

hope it helps you a lot...

2 Comments Leave new

Very Important program. I'll try with the C++ version too...

Reply

We can do it using linked lists with pointers too. by dynamic allocation to save memory

Reply

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.