简体   繁体   中英

duplicates in randomly generated array in Java

I am in a cs 2010 class. Haven't ever worked on coding before or anything of the sort. I have an okay teacher but he has a very thick accent that is hard to understand. He recently gave us a project to complete over a few days. I have been having problems getting the last part of the project done.

The project asks you to generate 10,000 random numbers between 0-9999 and arrange them in an array of 10,000 numbers without repeating any of them. As you can see, this is basically asking you to make the array put the numbers 0-9999 in an array in order of least to greatest. My problem is the non-repeating numbers. I have been working on the code for over 4 hours trying to figure out how to make it not repeat and have had no luck. I have searched online for at least an hour and all other hints or solutions have not helped. This is the code I have so far, can anyone please help me?

 package array.sorter.project;

import java.util.Arrays;
import java.util.Random;

public class Sorting {
public static void main(String args[]){
int[] randomNumbers = new int[10000];

Random rand = new Random();{
for (int i = 1; i < randomNumbers.length; i++) {
  int n = rand.nextInt(10000);
  randomNumbers[i] = n;}


  for (int i = 0; i < randomNumbers.length; i++) {
      int smallestNo = randomNumbers[i];
      int posWithSmallest = i;
      for (int j = i+1; j < randomNumbers.length; j++) {
        int val = randomNumbers[j];
        if (val < smallestNo) {
          smallestNo = val;
          posWithSmallest = j;
        }
      }
      int tmp = randomNumbers[i];
      randomNumbers[i] = smallestNo;
      randomNumbers[posWithSmallest] = tmp;
}
Arrays.sort(randomNumbers);

for (int i = 0; i < randomNumbers.length; i++) {
      System.out.println("Position " + i + " : " + randomNumbers[i]);
    }



    }

}

}

Instead of randomly generating 10000 numbers from 0 to 9999, generate 0...9999 in ascending order and shuffle the array. Make sure that your shuffling is unbiased, eg that there are n! ways it can complete (if you're not sure, desk check it with n = 3 to see if it is unbiased)

You can not generate 10000 random integers in range 0-9999 without duplicates, there are only 10000 of then, so you need all.

What you can do is to rearrange , shuffle them.

So:

  import java.util.Collections;
  import java.util.Arrays;

  ...
  int[] ten_thousand = new int[10000];
  for (int i=0; i < 10000; i+=1) ten_thousand[i] = i;
  return Collections.shuffle(Arrays.asList(ten_thousand));

Know your weapons :)

If you do not want to use shuffle

private static int[] generateRandom(int count) {
    int[] randomNumbers = new int[count];

    Set<Integer> checker = new HashSet<Integer>();

    Random rand = new Random();
    for (int i = 0; i < count;) {
        int nextInt = rand.nextInt(count);
        if (!checker.contains(nextInt)) {
            randomNumbers[i++] = nextInt;
            checker.add(nextInt);
        }
    }

    return randomNumbers;
}

I've written a O(n) algorithm to solve this problem inspired by the book Programming Pearls, 2nd Edition .the code is below,i will explain it later:

    /**
 * randomly select k numbers in [0,n),and sort them in random order.(k<=n)
 */
public static int[] getRandomArray(int n, int k) {
    if (k > n) {
        k = n;
    }
    int[] rets = new int[k]; // store the random ordered number
    int[] array = new int[n];// original array that array[i] is i
    for (int i = 0; i < n; i++)
        array[i] = i;
    Random random = new Random();
    for (int j = 0; j < k; j++) {
        // generate a random number between [j,n) as index
        int index = j + random.nextInt(n - j);
        // swap array[j] and array[index],so array[0..j] are all non-repeat
        // random number
        int temp = array[index];
        array[index] = array[j];
        array[j] = temp;
        // store it in rets
        rets[j] = temp;
    }
    return rets;
}

explain:

to generate non-repeating 10,000 random numbers between 0-9999 

can be considered to arrange number 0-9999 in random order 。 1,the k number are stored in array ,within which x in position x.

2,for the number j,random select a index from [j,n),that's index,

3,swap the position of j from j to index,(eq to swap the number at index to position j)

4,loop j from 0 to k,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM