I was wondering how do i go about generating unique random numbers?
i want to generate numbers between 0-9 randomly. I dont want to repeat the numbers.Then that no: should display on the buttons.ie there are 9 buttons. first randomly generated number should display on the first button & 2nd no: should display on 2nd button. & so on for example I have the numbers 9371482065 then 9 should display on the first button ,3 should display on 2nd button ....
But the following code i'm having is repeating some numbers...can any one help me in solving...please.....


        Random rgen = new Random();  // Random number generator
        int[] cards = new int[10];  

//      --- Initialize the array to the ints 0-51
        for (int i=0; i<10; i++) {
            cards[i] = i;

//      --- Shuffle by exchanging each element randomly
        for (int i=0; i<10; i++) {
            int randomPosition = rgen.nextInt(10);
            System.out.println(" randomPosition   " + randomPosition);
            int temp = cards[i];
            System.out.println(" temp" + temp);
            cards[i] = cards[randomPosition];
            cards[randomPosition] = temp;

            System.out.println(" Random Number" + cards[randomPosition]);


Edited by pyTony: fixed formatting

11 Years
Discussion Span
Last Post by neelkris

Are you creating the Random object each time that method is called? The Random object should bounce around and hit every number before repeating itself.


Suppose you want to generate 50 unique random numbers from 0 to 100.

//Create an array of size 100 that remembers which integers are taken
boolean[] taken = new boolean[100];

//start count at 0
int count =0;

//create random gen
Random r = new Random();

//create array to hold 50 unique random numbers
int[] uniqueRNums = new int[50];

//begin the loop, stop when got 50 unique random numbers
while(count < 50)
     //get random integer 0-100
     int rNum = r.nextInt(100);

     //check if it is taken
     if(!taken[rNum] )
        //mark as taken
        taken[rNum] = true;

        //add to unique random numbers list

        //update count

Hope this helps.

:?: For more help, www.NeedProgrammingHelp.com


Hi this is Program .. without Creating the Random object each time and without Boolean Array...

// Random 1-10 Numbers..
import java.lang.*;
import java.util.*;

class rand{
    public static void main(String args[]) {
        int arr[] = new int[10];
        int count = 0;
        boolean found;
        Random r=new Random();  

        while(count < 11) {
             int num=r.nextInt(10); 
            found = false;

            for(int i = 0; i < count; i++)
                if(arr[i] == num){
                    found = true;

                arr[count++] = num;

Edited by mike_2000_17: Fixed formatting


because it's a lazy kid that wants its homework done for free. It's also spamming me with PMs and resurrecting other threads.


Hello Sir,

I am doing a small mini project for my college and i need code on how to create a minimun of hundred buttons placed in a grid.

you will be a lifesaver if you can provide me with this little task

Thanking You.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.