Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. public static int[] uniqueRandomElements (int size) {
  2.  
  3. int[] a = new int[size];
  4.  
  5. for (int i = 0; i < size; i++) {
  6. a[i] = (int)(Math.random()*10);
  7.  
  8. for (int j = 0; j < i; j++) {
  9. if (a[i] == a[j]) {
  10. a[j] = (int)(Math.random()*10);
  11. }
  12. }
  13. }
  14.  
  15. for (int i = 0; i < a.length; i++) {
  16. System.out.print(a[i]+" ");
  17. }
  18. System.out.println();
  19. return a;
  20. }
  21.  
  22. for (int i = 0; i < size; i++) {
  23. a[i] = (int)(Math.random()*10);
  24.  
  25. for (int j = 0; j < i; j++) {
  26. if (a[i] == a[j]) {
  27. a[j] = (int)(Math.random()*10); //What's this! Another random number!
  28. }
  29. }
  30. }
  31.  
  32. for (int i = 0; i < size; i++) {
  33. a[i] = (int)(Math.random()*10);//note, this generates numbers from [0,9]
  34.  
  35. for (int j = 0; j < i; j++) {
  36. if (a[i] == a[j]) {
  37. i--; //if a[i] is a duplicate of a[j], then run the outer loop on i again
  38. break;
  39. }
  40. }
  41. }
  42.  
  43. ArrayList<Integer> a = new ArrayList<>(11);
  44. for (int i = 0; i <= 10; i++){ //to generate from 0-10 inclusive.
  45. //For 0-9 inclusive, remove the = on the <=
  46. a.add(i);
  47. }
  48. Collections.shuffle(a);
  49. a = a.sublist(0,4);
  50. //turn into array
  51.  
  52. ArrayList<Integer> list = new ArrayList<>(11);
  53. for (int i = 0; i <= 10; i++){
  54. list.add(i);
  55. }
  56. int[] a = new int[size];
  57. for (int count = 0; count < size; count++){
  58. a[count] = list.remove((int)(Math.random() * list.size()));
  59. }
  60.  
  61. while (true) {
  62. boolean need_to_break = true;
  63. for (int j = 0; j < i; j++) {
  64. if (a[i] == a[j]) {
  65. need_to_break = false; // we might get another conflict
  66. a[j] = (int)(Math.random()*10);
  67. }
  68. }
  69. if (need_to_break) break;
  70. }
  71.  
  72. int max_number = 10;
  73. int[] all_numbers = new int[max_number];
  74. for (int i = 0; i < max_number; i++)
  75. all_numbers[i] = i;
  76.  
  77. /* randomly permute the sequence */
  78. for (int i = max_number - 1; i >= 0; i--) {
  79. int j = (int)(Math.random() * i); /* pick a random number up to i */
  80.  
  81. /* interchange the last element with the picked-up index */
  82. int tmp = all_numbers[j];
  83. all_numbers[j] = a[i];
  84. all_numbers[i] = tmp;
  85. }
  86.  
  87. /* get the a array */
  88. for (int i = 0; i < size; i++)
  89. a[i] = all_numbers[i];
  90.  
  91. int [] arr = [1,2,3,.....(size)]; //this is pseudo code
  92.  
  93. Collections.shuffle(arr);// you probably need to convert it to list first
  94.  
  95. public Integer[] generateUnsortedIntegerArray(int numElements){
  96. // Generate an array of integers
  97. Integer[] randomInts = new Integer[numElements];
  98. for(int i = 0; i < numElements; ++i){
  99. randomInts[i] = i;
  100. }
  101. // Do the Knuth shuffle
  102. for(int i = 0; i < numElements; ++i){
  103. int randomIndex = (int)Math.floor(Math.random() * (i + 1));
  104. Integer temp = randomInts[i];
  105. randomInts[i] = randomInts[randomIndex];
  106. randomInts[randomIndex] = temp;
  107. }
  108. return randomInts;
  109. }
  110.  
  111. import java.util.Scanner;
  112. class Unique
  113. {
  114. public static void main(String[]args)
  115. {
  116. int i,j;
  117. Scanner in=new Scanner(System.in);
  118. int[] a=new int[10];
  119. System.out.println("Here's a unique no.!!!!!!");
  120. for(i=0;i<10;i++)
  121. {
  122. a[i]=(int)(Math.random()*10);
  123. for(j=0;j<i;j++)
  124. {
  125. if(a[i]==a[j])
  126. {
  127. i--;
  128.  
  129. }
  130. }
  131. }
  132. for(i=0;i<10;i++)
  133. {
  134. System.out.print(a[i]);
  135. }
  136. }
  137. }
  138.  
  139. public static ArrayList<Integer> noRepeatShuffleList(int size) {
  140. ArrayList<Integer> arr = new ArrayList<>();
  141. for (int i = 0; i < size; i++) {
  142. arr.add(i);
  143. }
  144. Collections.shuffle(arr);
  145. return arr;
  146. }
  147.  
  148. int[] a = new int[20];
  149.  
  150. for (int i = 0; i < size; i++) {
  151. a[i] = (int) (Math.random() * 20);
  152.  
  153. for (int j = 0; j < i; j++) {
  154. if (a[i] == a[j]) {
  155. a[i] = (int) (Math.random() * 20); //What's this! Another random number!
  156. i--;
  157. break;
  158. }
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement