Velocity's MathTool ($math
in Marketo's Script Editor) offers the convenience function [random()
](https://velocity.apache.org/tools/2.0/apidocs/org/apache/velocity/tools/generic/MathTool.html#random(java.lang.Object,\ java.lang.Object)) to generate a random number in a given range.
As I suggested to user LD in this Community thread, you can use random()
to randomly select content for quick-and-dirty A/B tests.
For example, this will choose one of 3 link titles at random for every lead in a send:
#set( $random = $math.random(0,3) )
#set( $linkText = {
0 : "Get the best rates here!",
1 : "Load your truck!",
2 : "Get moving!"
})
<a href="http://move.example.com/choosemover" id="linkText${random}">${linkText[$random]}</a>
random(0,3)
returns a random whole number between 0 inclusive and 3 exclusive, that is, either 0, 1, or 2.
Then you'd constrain Smart Lists by Link ID to compare results.
Try it out!
Breaking down MathTool.random()
The shortcut
$math.random(0,3)
gives the same result as
$math.floor( $math.mul( $math.getRandom(), 3 ) )
So random()
saves some keystrokes/verbosity in your code, but you could do the same in more “raw” Velocity as well.
Also, if you were extremely pedantic about randomness, you would point out that across different Velocity contexts (and potentially across different JVMs in Marketo's server farm) you aren't using the same seeded PRNG, so even the (P)seudo quality can't be trusted.
However, for martech applications we clearly have acceptable randomization — we aren't exactly running a pandemic simulation or wargame here.☺