Getting random number in Salesforce Apex

Posted on 14-02-2013 09:11 by graham
This tutorial shows how to get random integer and double values in Apex.

Random double between 0.0 and 1.0

Getting random double values is easy and can be done using the Math.random method.
Double randomNumber = Math.random();

The result will be a number greater or equal to 0.0 and less than 1.0.

Getting a random integer

To get a random integer, use the following method:
public class RandomUtils
* Gets a random integer number between lower (inclusive) and upper (exclusive)
public static Integer getRandomInt (Integer lower, Integer upper)
return Math.round(Math.random() * (upper - lower)) + lower;

The getRandomInt method will generate a random number greater or equal to lower and less than upper.
hey! thanks! there's an extra bracket in there though.... should be this:

return Math.round((Math.random() * (upper - lower)) + lower;
Added on 27-06-2013 21:40 by anonymous
The output here doesn't quite match the description. With math.round() being used, this will sometimes round up, so rather than always getting "less than upper" - this will give you the upper value.

Further, this won't produce a uniform distribution of random numbers unfortunately, with the lower and upper values appearing less often than those in between. This occurs as when a double is generated (and multiplied by n = [upper - lower]), if the result is between 0.5 higher and 0.5 lower than a given number x (of course, lower <= x <= upper), the round will go towards x - except for the upper and lowers. For the upper bound, the upper bound will only be the result if the random number is 0.5 less than the upper bound (and likewise for the lower bound - you will get the lower bound only when the random number is up to 0.5 greater than the lower bound).This means you get the upper and lower bounds half as often as the numbers in between.

Using math.floor() will solve both of these problems.

First, math.floor() means that you will never get the upper bound. Math.random() generates LESS than 1.0, so will always round down to 0; ergo math.random() * n will always round down to n - 1.

This also gives a uniform distribution. When the random double * n (n = [upper - lower]) is generated, for a given number x (lower <= x < upper) the math.floor() function will result in x if the random number is 0 to 0.99... higher than x. This is true for all values of x. The upper value will never be reached as math.random() never generates 1 as explained above, and the lower bound has an equal chance of selection compared to any other value of x

So the new line should be
return (Integer) (Math.floor(Math.random() * (upper - lower)) + lower);
Added on 04-02-2014 02:19 by anonymous


Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems