Random Numbers on button


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Random Numbers on button

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    22

    Random Numbers on button

    Hi,

    I was wondering how do i go about generating unique random numbers?
    ie
    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.....


    code::::


    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]);

    }

  2. #2
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    Computers can't generate real random numbers but you can get some real random numbers here http://www.fourmilab.ch/hotbits/

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I think this is random enough for your purpose

    The idea is to stuff the random numbers into a HashSet as you go along.
    When the size of the hashset is 10 you know that it contains 10
    unique integers.

    Code:
    import java.util.*;
    
    public class Ramdomizer {
      public static Random rand=new Random(System.currentTimeMillis());
      static HashSet hs=new HashSet();
    
      public Ramdomizer() {}
    
      public int [] getRandoms(int maxNo) {
        hs.clear();
        while (true) {
          int n = rand.nextInt(maxNo);
          hs.add(new Integer(n));
          if (hs.size() == maxNo) {
            break;
          }
        }
        int [] ret=new int[maxNo];
        Iterator it=hs.iterator();
        int cnt=0;
        while (it.hasNext()) {
          ret[cnt++]=((Integer)it.next()).intValue();
        }
        return shuffle(ret);
      }
    
      private int [] shuffle(int [] arr) {
        for (int i=0; i<arr.length; i++) {
          int rp = rand.nextInt(10);
          int tmp=arr[rp];
          arr[rp]=arr[i];
          arr[i]=tmp;
        }
        return arr;
      }
      /**
       * *********** MAIN ***********
       */
      public static void main(String[] args) {
        Ramdomizer rd = new Ramdomizer();
        int [] rn=rd.getRandoms(10);
        showResult(rn);
        rn=rd.getRandoms(10);
        showResult(rn);
      }
      private static void showResult(int[] rn) {
        for (int i=0; i<rn.length; i++) {
          System.out.println(i+": "+rn[i]);
        }
      }
    }
    Last edited by sjalle; 10-07-2005 at 10:46 AM.
    eschew obfuscation

  4. #4
    Join Date
    Oct 2005
    Posts
    40

    Lightbulb

    Quote Originally Posted by daina
    Hi,

    I was wondering how do i go about generating unique random numbers?
    ie
    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.....


    code::::


    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]);

    }

    Read your code again. Its perfect! You just need to print the cards array in another loop after the first for loop and you will get desired result.

    Regards,
    Mohit

Similar Threads

  1. Database & Random Numbers
    By Suze in forum VB Classic
    Replies: 2
    Last Post: 05-10-2005, 04:41 PM
  2. button listener not working
    By DrunkinP in forum Java
    Replies: 0
    Last Post: 03-31-2005, 10:36 AM
  3. generating unique random numbers
    By Dave in forum Java
    Replies: 3
    Last Post: 05-14-2002, 09:04 PM
  4. random numbers
    By max in forum Java
    Replies: 2
    Last Post: 04-30-2001, 04:08 PM
  5. Replies: 0
    Last Post: 08-25-2000, 08:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center