import java.lang.Math;
public class PhraseOMatic{
public static void main(String[]args){
//Make three set of words to choose from. Add your own!
String[] wordListOne = {"24/7", "multi-Tier","30,000 foot","B-to-B","win-win","front-end","web-based","pervassive","smart","six-sigma","critical-path","dynamic"};
String[] wordListTwo = {"empowered","sticky","value-added","oriented","centric","distributed","clustered","branded","outside-the-box","positioned","networked","focused","leveraged","aligned","targeted","shared","cooperative","accelerated"};
String[] wordListThree={"process","tippin-point","solution","architecture","core competency","strategy","mindshare","portal","space","vision","paradigm","mission"};
// find out how many words in each list
int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.length;
//generate three random numbers
int rand1 =(int) (Math.random()* oneLength);
int rand2 = (int) (Math.random()* twoLength);
int rand3 =(int) (Math.random()* threeLength);
// now build a phrase
String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
//print out the phrase
System.out.println("What we need is a "+ phrase);
}
}
I am not getting the code snippet //generate three random numbers (I need explanation on working of Math.random()*oneLenghth and so on....)
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Misiu
23 October 2020, 11:12
Math.random() results as double type number: 0 <= ...... < 1, eg. 0.71.....
Math.random() * oneLength results as Math.random() * 12, what results as 8.52... (double type)
(int) (Math.random()* oneLength) results as 8 (integer type)
Look at:
https://www.geeksforgeeks.org/java-math-random-method-examples/ (Example 2)
0