6 Replies Latest reply on Apr 25, 2009 4:16 PM by 807588

# 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 :">
Thanks!
• ###### 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``````
--janeiros
• ###### 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.

http://i351.photobucket.com/albums/q461/luuthithanhhoa/switch2.jpg

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

http://i351.photobucket.com/albums/q461/luuthithanhhoa/switch1-1.jpg

``````/**
* @(#)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();
setLayout(gl);
Random     x = new Random();
button = new JButton[11];    // to store numbers;
HashSet<String> hSet = new HashSet<String>();
int i = 0;
do
{

int k = 1+x.nextInt(9);
String l = ((Integer)(k)).toString();
if(hSet.contains(l)==false)
{
button[i] = new JButton(l);  //  random numbers;
i++;
}

}while(i<9);

}

public void actionPerformed(ActionEvent event)
{
container.setLayout(gl);
container.validate();
}

public static void main(String args[])
{
NumberButton nb = new NumberButton();
nb.setSize(200,200);
nb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nb.setVisible(true);
}

}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()
e.g.
``````List<Integer> nums = new ArrayList<Integer>();
for(int i=0;i<1000;i++)