# What is the function of Math.random(), rand1, rand2 and rand3 in this code?

 1 2 public class PhaseOMatic { public static void main(String args[]) { String[] wordListOne= {"24/7","multi-tier","30,000foot","B-to-B","win-win","frontend","web-based","pervasive","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","tipping-point","solution","archtecture","corecomputing","strategy","mindshare","portal","space","vision","paradigm","mission"}; int oneLength=wordListOne.length; int twoLength=wordListTwo.length; int threeLength=wordListThree.length; int rand1=(int)(Math.random()*oneLength); int rand2=(int)(Math.random()*twoLength); int rand3=(int)(Math.random()*threeLength); String phrase=wordListOne[rand1]+" "+wordListTwo[rand2]+" "+wordListThree[rand3]; System.out.println("What we need is a "+phrase); } } In this code I did not understand Math.random(), rand1, rand2 and rand3. asked 15 May '16, 15:59 Sami 21●4 manetsus 2.2k●2●11

 0 In the javadoc of Math.random(), it is written that: public static double random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. That means multiplying something with it would result a floating-point value greater than or equal to 0.0 and less than the multiplied number. By casting the result to int just truncate the decimal part. So, it could easily be used as a random index having length of that multiplied number. In this way three random index rand1, rand2 and rand3 are chosen from three different lists of strings having different number of items. You can print the values coming from Math.random() by taking it in double variable for better understanding. Feel free to ask anything you could not understand. Thanks. answered 16 May '16, 01:19 manetsus 2.2k●2●11
 toggle preview community wiki:

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Question tags:

×215
×69

question asked: 15 May '16, 15:59

question was seen: 1,512 times

last updated: 16 May '16, 01:22