Reputation: 23
Here is a piece of code that I am working with, however, I want to generate random responses with regards to the strings that I have set up. How best to approach this? Perhaps just to clarify, I need my "Hello", "Good day", "bye", "farewell" to be output randomly as opposed to the order in which they are listed.
public void run() {
String importantInfo[] = {
"Hello",
"Good day",
"bye",
"farewell"
};
Random random = new Random();
for (int i = 0; i < importantInfo.length; i++) {
drop.put(importantInfo[i]);
try {
Thread.sleep(random.nextInt(5000));
} catch (InterruptedException e) {}
}
drop.put("DONE");
}
Upvotes: 2
Views: 796
Reputation: 21
Go get apache-commons for RandomUtils and call nextInt(4).
There are also random string , long, and float methods available.
Upvotes: 2
Reputation: 8348
Presuming "strings I have set up" refers to the importantInfo variable, and random response is a random item from that array: select a random number from the Random object and use this value as the index to your string array.
String randomString = importantInfo[random.nextInt(importantInfo.length)];
Upvotes: 7