Here is my program which generates random name:
String[] names = new String[]{"one","two","three","four","five"}; Random rd = new Random(); int x = rd.nextInt(names.length);
I need it to generate the names based on last weekend check, if the generated name matches with last weekend’s generated name then the program to run again to generate different name. Ex: If “one” is generated on Saturday and “two” is generated on Sunday, then next week these two should be excluded from generation. How can i make that check in Java?
Advertisement
Answer
I need it to generate the names based on last weekend check, if the generated name matches with last weekend’s generated name then the program to run again to generate different name.
If this doesn’t work for you it may give you some ideas.
- choose two names at random to be weekend names.
- remove those names from future use.
- next time, choose new names from available list
- add back the old names
- and update the current names in use.
Now two names will appear in adjacent rows as the currently used names are printed.
import java.util.ArrayList; import java.util.List; import java.util.Random; public class WeekEndCheck { List<String> lastWeekendNames = new ArrayList<>(); List<String> availableNames = new LinkedList<>( Arrays.asList("one", "two", "three", "four", "five")); public static void main(String[] args) { new WeekEndCheck().start(); } public void start() { // do it for 10 weekends, not two names should // appear adjacent to each other for (int i = 0; i < 10; i++) { setWeekEndNames(); System.out.println("Weekend " + (i+1) + " " + lastWeekendNames); } } Random r = new Random(); public void setWeekEndNames() { List<String> temp = new ArrayList<>(); for (int i = 0; i < 2; i++) { int indx = r.nextInt(availableNames.size()); temp.add(availableNames.remove(indx)); } availableNames.addAll(lastWeekendNames); lastWeekendNames.clear(); lastWeekendNames.addAll(temp); } }
Prints something like this
Weekend 1 [three, one] Weekend 2 [two, four] Weekend 3 [five, one] Weekend 4 [two, four] Weekend 5 [one, three] Weekend 6 [five, four] Weekend 7 [three, two] Weekend 8 [one, four] Weekend 9 [two, five] Weekend 10 [one, three]
Update
To permit the same name on both Saturday and Sunday but not be used the following weekend, this should work. It is similar to the other except:
- the indices are saved and conditionally removed after the names have been selected.
- if the two indices are the same, only one name is removed so it can’t be used the following weekend
- otherwise, to keep the list in sync, first remove the larger index number, then remove the smaller. Otherwise, the wrong one could get removed or an exception could be thrown.
public void setWeekEndNames() { List<String> temp = new ArrayList<>(); int[] indices = new int[2]; for (int i = 0; i < 2; i++) { indices[i] = r.nextInt(availableNames.size()); temp.add(availableNames.get(indices[i])); } if (indices[0] == indices[1]) { availableNames.remove(indices[0]); } else { availableNames.remove(Math.max(indices[0], indices[1])); availableNames.remove(Math.min(indices[0], indices[1])); } availableNames.addAll(lastWeekendNames); lastWeekendNames.clear(); lastWeekendNames.addAll(temp); }