6 Replies Latest reply: Apr 25, 2009 11:16 AM by 807588 RSS

    How to generate diffrent random  numbers

      Hi everybody.
      I'm programing a mini game and i think i have to generate diffrent random numbers to do it. I think that each time i generate a random number, i should store it and the next time generate random number, i check it out wheater it is exist or not. But i don't know how to check.
      Can you help me.
      Sorry because of my bad English :">
        • 1. Re: How to generate diffrent random  numbers
          When you say random number, do you mean random decimal between 0 and 1? Do you mean an integer between 1 and 10?

          When you say they have to be unique, how unique do they have to be? Are .123456 and .1234567 unique enough?
          • 2. Re: How to generate diffrent random  numbers
            Do you want random numbers or guaranteed unique numbers in a (semi)random sequence.
            Quite different principles, as with random numbers there's no guarantee that you'll get a different one each time.
            • 3. Re: How to generate diffrent random  numbers
              Once you generate the number you could use a HashSet or a TreeSet (if you need order) to store them.
              // Create the set
              Set set = new HashSet();
              // Add elements to the set
              // Adding an element that already exists in the set has no effect
              size = set.size();              // 3
              // Determining if an element is in the set
              boolean b = set.contains("a");  // true
              b = set.contains("d");          // false
              • 4. Re: How to generate diffrent random  numbers
                It depends on what kind of range of numbers. For example, if you want only numbers from 1 - 10 then load a list with the numbers 1-10 and use Collections.shuffle on it.

                If the range is large then use a Set to store the values already hit and, on a duplicate, get the next number from the Random sequence.
                • 5. Re: How to generate diffrent random  numbers
                  Thank you for your helps, i have finished it.
                  But i have a broblem.
                  I've just studied little about interface.
                  I can't create an empty button in and reset Button in Frame like this.


                  When the user click button reset. It will be rearranged like this


                   * @(#)NumberButton.java
                   * @Phuong Thao.
                   * @version 1.00 2009/4/20
                  import java.awt.event.ActionEvent;
                  import java.awt.event.ActionListener;
                  import javax.swing.*;
                  import java.awt.*;
                  import java.util.*;
                  public class NumberButton extends JFrame implements ActionListener{
                           JButton button[];
                           GridLayout gl;
                           Container container;
                           public NumberButton()
                           {     super("Switch numbers");
                                gl = new GridLayout(3,3);
                                container = getContentPane();
                                Random     x = new Random();
                                button = new JButton[11];    // to store numbers;
                                HashSet<String> hSet = new HashSet<String>();
                                int i = 0;
                                     int k = 1+x.nextInt(9);
                                     String l = ((Integer)(k)).toString();
                                          hSet.add(l);                       // to store diffrent
                                          button[i] = new JButton(l);  //  random numbers;
                       public void actionPerformed(ActionEvent event)
                       public static void main(String args[])
                       NumberButton nb = new NumberButton();

                  Each time the user click a button that there is a above, beside or bottom empty buttom. It will be switch to this button randomly. How can i do this?
                  Edited by: sunflower2222 on Apr 25, 2009 12:43 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                  • 6. Re: How to generate diffrent random  numbers
                    If you want a random set of unique numbers you can use Collections.shuffle()
                    List<Integer> nums = new ArrayList<Integer>();
                    for(int i=0;i<1000;i++)
                    // print 1000 unique numbers in random order.
                    for(int i=0;i<1000;i++) {
                       int randomNum = nums.remove(nums.size()-1);