In: Computer Science
please code in java:
There's an arraylist of "words" with 250,000 words. Pick 50,000 randomly chosen words from "words" and add them to a "search" array (not Arraylist) of strings.
Thanks for the question, selecting 50,000 words from 250,000 words is like selecting 1 word for every 5 words.
We can divide the 250,000 words into 50,000 groups where each group contains 5 words, and then selecting one word from each group.
First group starts with index 0 and ends with index 4
Second group starts with index 5 and ends with index 9
...
...
Likewise and so forth.
Below is the code that you will be needing. Let me know for any questions or help.
===========================================================================
import java.util.ArrayList;
import java.util.Random;
public class RandomSearchWords {
public static void main(String[] args) {
ArrayList<String> words = new ArrayList<>(250000);
Random random = new Random();
String search[] = new String[50000];
int index = 0;
int randomIndex = 0;
for (int i = 0; i < search.length; i++) {
randomIndex = i * 5 + random.nextInt(5);
search[i] = words.get(randomIndex);
}
}
}
==========================================================================
thanks !