Random number within Range without Repetation




/*
 * Random number within Range without Repetation
 */
package random;

import java.util.*;

public class DemoRandom
{
    public static void main(String[] args)
    {
        Random r=new Random();
        int i;
        ArrayList<Integer> arr=new ArrayList<Integer>();
        for(i=1;i<=10;i++)
        {
            arr.add(i);
        }  
        int cnt=arr.size();
        for(i=0;i<cnt;i++)
        {
         
            int z=r.nextInt(arr.size());
            System.out.println((arr.get(z)));
            arr.remove(z);
        }
        System.out.print(cnt+"");
    }
}


OUTPUT
5
8
3
4
2
6
10
9
1
7

Total :10

Random number within Range without Repetation Random number within Range without Repetation Reviewed by Unknown on 12:02 pm Rating: 5

No comments: