informaticage

random no repeat array nb

Dec 3rd, 2021 (edited)
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. import java.util.*;
  2. public class MyClass {
  3.     public static void main(String args[]) {
  4.         int[] numbers = new int[30];
  5.         int generated = 0;
  6.        
  7.         while(generated != 30) {
  8.           boolean tonno = false;
  9.           int random = (int)(Math.random() * 100);
  10.           for(int i = 0; i < 30; i++) {
  11.               if(numbers[i] == random) {
  12.                   tonno = true;
  13.               }
  14.           }
  15.          
  16.           if(!tonno) {
  17.               numbers[generated] = random;
  18.               generated = generated + 1;
  19.           }
  20.         }
  21.      
  22.      
  23.         System.out.println("Generated: ");
  24.         for(int i = 0; i < 30; i++) {
  25.           System.out.print(numbers[i] + " ");
  26.         }
  27.     }
  28. }
Add Comment
Please, Sign In to add comment