Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
BigP

# Can someone explain these lines of code?

Integer count = [SELECT COUNT() FROM Contact];
Integer rand = Math.floor(Math.random() * count).intValue();
Contact c = [SELECT Name FROM Contact LIMIT 10 OFFSET :rand];

Sai Praveen (Salesforce Developers)
Hi,

Please find the explanation  for the code.

```Integer count = [SELECT COUNT() FROM Contact]; // This is to get the count of contacts in org.
```

Math.random() function will generate the random number greater than 0 and less than 1

Math.floor() function will  Returns the largest (closest to positive infinity) Decimal that is not greater than the argument and is equal to a mathematical integer.

intValue() is used to calculate the integer value of the decimal.

`Integer rand = Math.floor(Math.random() * count).intValue();// is generating a random number based on the count.`
OFFSET is used to skip those many values.

```Contact c = [SELECT Name FROM Contact LIMIT 10 OFFSET :rand]; // if the rand variable from above step returns 5 then it will give the result of contact from 6 to 15.
```
Let me know if you face any issues.