function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Random numbers in Apex?

Hi there,

I'm trying to best determine how to randomly segment my contacts.  Is there a way to generate a random number in Apex to update all or selected contacts?

I have little to no success with 'true' random number generation with formulaic fields.

CRMfusion - GWCRMfusion - GW
You could find the current DateTime and then create a function that utilizes the smallest time portion as a seed.

You may be better off looking at the ContactID.  As it is added sequentially you could use the last 1 or 2 characters to assigne a psedu - random distribution of the contacts.

Ron HessRon Hess
Yes, in Apex code there is

    Double rnd =  Math.random();

which you can then use to pick an index from an array of contacts.

I created a randomization class in APEX that might do what you need. You can download the class and see useage examples on my blog:


Hope this helps,