+ Start a Discussion
Veena GopalVeena Gopal 

Units_Sold__c = (Double)(10*Math.random()+1));

In Apex workbook in page 52, they have assigned a value to the field as below.
Units_Sold__c = (Double)(10*Math.random()+1));

what does this mean? what value will be stored in the field Units_Sold__c?
Best Answer chosen by Veena Gopal
Amit Chaudhary 8Amit Chaudhary 8
Math.random()
Returns a positive Double that is greater than or equal to 0.0 and less than 1.0.
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_math.htm

So Random number will give number from 0.0 to 1.0 .
* 10 means 0 to leass then 10.
Then +1 means 1 to less the 11 ( means 10.999)

User-added image
You can simply execute the same code in Developer console and Workbench to see the result.

 

All Answers

Shiva RajendranShiva Rajendran
Hi Veena ,

Math.random() is a random number generator. It returns any random decimal number between 0.0 to less than 0.1. 
Now (10 * Math.random()) gives some double numbers between 0 - 10. so 10*Math.random()+1 gives 1 - 10.9.
Hope it helps.
Thanks and Regards,
Shiva RV
 
Amit Chaudhary 8Amit Chaudhary 8
Math.random()
Returns a positive Double that is greater than or equal to 0.0 and less than 1.0.
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_math.htm

So Random number will give number from 0.0 to 1.0 .
* 10 means 0 to leass then 10.
Then +1 means 1 to less the 11 ( means 10.999)

User-added image
You can simply execute the same code in Developer console and Workbench to see the result.

 
This was selected as the best answer