I am trying to figure out how to generate and store 10 random numbers in array where the numbers are two digit and each digit is in the range of 0-7. For example, 10, 23, 35, 77 are all ok, but not 1,78,89,99. And also, I want to make sure that all the numbers are unique. Here is what I have come up to so far...
import java.util.Random;
public class RandomNum{
public static void main(String[] args){
Random rand=new Random();
int[] randomFirstDigit=new int[10];
int[] randomSecondDigit=new int[10];
for(int i=0;i<10;i++){
randomFirstDigit[i]=rand.nextInt(7-1+1)+1;
}
for(int i=0;i<10;i++){
randomSecondDigit[i]=rand.nextInt(7-1+1)+0;
}
int[] randomArr=new int[10];
for(int i=0;i<10;i++){
randomArr[i]=(randomFirstDigit[i]*10)+randomSecondDigit[i];
}
for(int i=0;i<=randomArr.length;i++){
System.out.println(randomArr[i]);
}
}
}
The main issue with the above code is, sometimes, the array value is not unique. In other words, two identical numbers are stored in the array like 23,23.
Could any one please help me figure out the problem.
Thanks in advance for your help.
Aucun commentaire:
Enregistrer un commentaire