برنامه ای به زبان جاوا که n عدد تصادفی بدون تکرار را پیدا کند و در آرایه ای ذخیره کند.
//Unrepeated Random Numbers //Java Programming //By Sina Moradi //Published by: Marjan //Website: Samiantec.ir import java.util.*; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter First Number:"); int first=input.nextInt(); System.out.println("Enter Last Number:"); int last=input.nextInt(); System.out.println("Enter Count:"); int count=input.nextInt(); int a[]; try { a = generateRandoms(first, last, count); for (int i=0;i < count;i++) System.out.println(a[i]); } catch (Exception e) {System.out.println(e.getMessage());} } public static boolean isInArray(int a[], int last, int x) { for (int i=0;i < last;i++) if (a[i] == x) return true; return false; } public static int[] generateRandoms(int from, int to, int count) throws Exception { if(toto-from+1) throw new Exception("Count is high."); int a[]=new int[count]; int r; for (int i=0;i < count;i++) { do { r = (int)(Math.random() * (to-from+1) + from); } while(isInArray(a, i, r)); a[i] = r; } return a; } }