0

i need an array contains 10 elements with no repeating integers.. so my question is how to make an if condition to check everytime if the next random value is in the array or not.. i need a simple code i'm just a beginner here is my code

public static void main(String[] args) {
    int []a = new int [10];

    for (int i =0 ; i< 10 ; i++)
    { 
        a[i]= (int)(Math.random()*100)+1;
        System.out.println(a[i]);
    }

}
alexgbelov
  • 3,032
  • 4
  • 28
  • 42
Sami hanna
  • 49
  • 1
  • 1
  • 5
  • http://stackoverflow.com/questions/158716/how-do-you-efficiently-generate-a-list-of-k-non-repeating-integers-between-0-and – P. Str Aug 11 '16 at 17:52
  • I believe the OP is a beginner of sorts. Incase you need help @Samihanna here's the code you're looking for: http://ideone.com/mmSojn – gabbar0x Aug 11 '16 at 18:00
  • You can use a hashset to store numbers. A set only stores unique entities. Keep adding stuff until the limit '10' is reached, and finally convert this to an array and you're done. – gabbar0x Aug 11 '16 at 18:01
  • If you're just beginning, here's the algorithm I would use: before inserting the number at i, check the values of the array from 0 to i-1. If any of them match the number you want to insert, generate a new random number and repeat the process. This isn't the most efficient way of doing it, but its a good learning experience. Look up while loops and break statements. – alexgbelov Aug 11 '16 at 19:17
  • `int[] a ={0,1,2,3,4,5,6,7,8,9}; java.util.Arrays.shuffle(a);` The values are not random, but their position yes. – Olivier Grégoire Aug 11 '16 at 19:51

0 Answers0