I was making a Random number (sort of a guessing game) and have come up with the ff. code to generate 10 one or two-digit numbers(1 or 10 up to 40):
public void generate()
{
for(int i=0; i<=1; i++)
{
for(int l=0; l<10; l++)
{
Random rdm=new Random();
arr[l] = rdm.nextInt(range)+1;
}
}
}
However, this code only answers the need to generate 10 random one or two-digit numbers. I need to make this program generate unique random numbers. How can I do that?
sorry for the late update... what I want to do with this program is that if the array contains a duplicate, that duplicate would be replaced with a unique one...
==============SOLVED================
NEW PROBLEM:
HashSet set=new HashSet();
Random random=new Random();
while(set.Size()<10)
{
set.add(random.nextInt(range)+1);
}
lbtest.setText(set.toString());
bgen.setEnabled(false);
gametext.setText("");
As requested by ggrigery:
here's the updated code in reference to ggrigery's suggestion.
ArrayListfor example, to store previously generated numbers and then check to see if the new number is in that list.