public class Speak
{
private static String[] words_1 = {"Mr X","Mr Y","Mr Z"};
private static String[] words_2 = {"your","my","their","the"};
private static String[] words_3 = {"best friend","lunch"};
private static String[] words_4 = {"is behind you","is crying"};
public static void main(String[] args)
{
System.out.println(getRandomString(words_1));
System.out.println(getRandomSentence());
}
static String getRandomString(String selectedArray[])
{
return selectedArray[(int) Math.random()*selectedArray.length];
}
static String getRandomSentence()
{
return (getRandomString(words_1)+", "+getRandomString(words_2)+" "+getRandomString(words_3)+" "+getRandomString(words_4));
}
}
HALP.
How do I make it give me a fucking randomized sentence?
Make sure your random number is less than or equal to your array size.
You're casting the random number to an int before it's multiplied by the array length so it just floors to zero each time. Stick some brackets round the multiplication and it works
(int) (Math.random()*selectedArray.length)
use the fucking random class with nextInt(), dumbass
>>52138320
I think you are pretty spot on but...
Not sure how to implement that.
>>52138157
Poo in loo, Pajeet.
>>52138307
this is the answer
>>52141394
Yea it worked out pretty well for me.
But it isn't as clean as the nextInt()
>>52141692
I prefer doing things myself. Often just less overhead.