3 Replies Latest reply: Aug 22, 2008 1:47 PM by 843785 RSS

    array of unique random numbers - help?

      basically, i'm trying to create a BINGO card.

      i stared learning Java a week ago and am finding it easy to learn and use....at least so far.

      my instructor gave me the assignment to created a BINGO card. so i used the system.out.format command to make 5 tab fields, 8 spaces each ("%8s %8s %8s %8s %8s", "B", "I", "N", "G", "O"). although the assignment only wanted me to think numbers out of thin air, just to use as examples, i went above and beyond with the idea to make a BINGO card generator that could actually function.

      then i started the random number sequences, using the Math.random() command. all told, there are 24 of these in the program, one for each number on a bingo card. the field in the middle is the FREE space, so it has no Math.random command.

      in BINGO, each letter (one of my five tab fields) can have a number in ranges of 15. B is 1 to 15, I is 16 to 30, etc. it looks similar to this:

      B I N G O
      9 19 39 57 66
      3 28 32 51 74
      3 29 FREE 46 70
      14 28 43 55 67
      9 24 35 59 62

      as you can tell, i'm having trouble with actually making unique random numbers so that none repeat.

      is there a command or string or something to help me accomplish this?
        • 1. Re: array of unique random numbers - help?
          1. Check out [java.util.Random|http://java.sun.com/javase/6/docs/api/java/util/Random.html] -- it's better than Math.random.
          2. The easiest way to get random numbers without repetition is the same way you play cards: shuffle.
          3. That method requires you have a list, not an array, but it's easy to create an array backed by a list, for example:
          String[] data = ...
          List<String> dataList = Arrays.asList(data);
          [Collections framework tutorial.|http://java.sun.com/docs/books/tutorial/collections/index.html]
          • 2. Re: array of unique random numbers - help?
            The best way I've come up with is to use an object to store the numbers that implements Collection--like ArrayList...

            Then you load it with the range of numbers that you want and call, shuffle() on the object, that will randomize your range of numbers and then you can choose the quantity you want from the storage object. So let's say you need 25 number in the range of 1 to 100:

            Add the numbers (you have to use classes so I would do Integers) 1 to 100;
            call shuffle()
            pull back the first 25 numbers

            This will guarantee that each number is distinct and in a random order.
            If you need multiple sheets populated, you can just call shuffle() between population of each sheet.
            package Junk;
            import java.util.ArrayList;
            import java.util.Collections;
            class Junk{
              private ArrayList<Integer> l;
                l = new ArrayList<Integer>();
              public void loadList(int s, int e){
                for(int i=s; i<=e; i++){
                  l.add(new Integer(i));
              public void randomizeList(){
              public void printList5(){
                for(int i=0; i<5; i++){
              public static void main(String[] args){
                Junk j = new Junk();
            • 3. Re: array of unique random numbers - help?
              Argh! should have done a refresh before posting.