SOLVED

Go to solution
Level 3

## MathTool to Get Random Number

I'm trying to create a velocity script that outputs different text randomly (for testing copy in snippets) so my intention is to randomly generate a number and if it is even then output one line versus the other. If I can get it working then I'd add more content with different tracking so I know which one is generating response.

I cobbled together this script that isn't working and I'm not sure why?

`#set (\$random = \$math.getRandom())#if(\$random / 2 == 0) #set ( "The Most Loads<br />The Best Rates")#else #set ( "637,000 Loads<br />Posted Daily")#end`
Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Level 10 - Community Moderator

## Re: MathTool to Get Random Number

 `#set ( "The Most Loads
The Best Rates")`

This will always be a syntax error.

The Velocity #set command requires a left-hand side and a right-hand side.

If all you're trying to do is output, you don't need any directives at all. Any plain text is printed as-is, that's the way VTL works.

`The Most Loads<br />The Best Rates#set (\$random = \$math.getRandom()) #if(\$random / 2 == 0) `

The only result from getRandom() that, divided by 2, equals 0, is: 0.

This means your distribution is not 1:1, it's more like (1.7976931348623157 x 10308 + 4.9406564584124654 x 10324 -1) : 1*, or shall we say, as unevenly distributed as possible!

You want this:**

`#set (\$random = \$math.random(0,2))#if(\$random == 0) The Most Loads<br />The Best Rates#else 637,000 Loads<br />Posted Daily#end`

* I think I have this ratio right, if not, the thrust is the same, there's only one double value that can be proven to match n / 2 == 0

** Because the random seed is not guaranteed to be the same, you're actually using different PRNGs over time, but it's perfectly fine for this task.

5 REPLIES 5
Level 9 - Champion Alumni

## Re: MathTool to Get Random Number

Hey Liz,

QQ, even if you can enable the process you outlined above, how do you plan to track the performance of the different text?

Is there a reason you didn't just use an email A/B Test? You could do this by cloning the snippet, updating the clone and duplicating the email with the new snippet.

This process may yield better metrics to report on the success of text 1 compared to the other.

Just a thought... and interested in knowing how you plan to track performance in your proposed plan above.

Sincerely,

Keith Nyberg

Level 3

## Re: MathTool to Get Random Number

Hi, just that these ads will be in multiple emails and I want results from all. I'm planning (maybe unwisely so) to put the html with links in each option with a specific term that I'll track in GA. We don't have revenue explorer so the reporting I get on email content is limited. I wish there were a way to test on the snippet level.

Level 9 - Champion Alumni

## Re: MathTool to Get Random Number

Hey Liz,

Makes sense when the snippets will be used all over the place. Maybe just clone an email that you know gets sent really frequently and A/B test the snippet with just that email?

Ability to A/B test snippets could be a cool idea to submit as well.

Sincerely,

Keith Nyberg

Level 10 - Community Moderator

## Re: MathTool to Get Random Number

 `#set ( "The Most Loads
The Best Rates")`

This will always be a syntax error.

The Velocity #set command requires a left-hand side and a right-hand side.

If all you're trying to do is output, you don't need any directives at all. Any plain text is printed as-is, that's the way VTL works.

`The Most Loads<br />The Best Rates#set (\$random = \$math.getRandom()) #if(\$random / 2 == 0) `

The only result from getRandom() that, divided by 2, equals 0, is: 0.

This means your distribution is not 1:1, it's more like (1.7976931348623157 x 10308 + 4.9406564584124654 x 10324 -1) : 1*, or shall we say, as unevenly distributed as possible!

You want this:**

`#set (\$random = \$math.random(0,2))#if(\$random == 0) The Most Loads<br />The Best Rates#else 637,000 Loads<br />Posted Daily#end`

* I think I have this ratio right, if not, the thrust is the same, there's only one double value that can be proven to match n / 2 == 0

** Because the random seed is not guaranteed to be the same, you're actually using different PRNGs over time, but it's perfectly fine for this task.

Level 10 - Community Moderator

## Re: MathTool to Get Random Number

P.S. In case it isn't clear, Velocity's \$math.random wrapper is akin to (in pure Velocity):

`\$math.floor( \$math.mul( \$math.getRandom(), 2 ) )​`

Needless to say, the wrapper method creates much less clutter!