SOLVED
## MathTool to Get Random Number

Go to solution

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-18-2018
09:00 AM

01-18-2018
09:00 AM

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

Solved! Go to Solution.

Labels:

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-18-2018
12:34 PM

01-18-2018
12:34 PM

#set ( "The Most Loads<br />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 10^{308} + 4.9406564584124654 x 10^{324 }-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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-18-2018
10:14 AM

01-18-2018
10:14 AM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-18-2018
10:22 AM

01-18-2018
10:22 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-18-2018
10:27 AM

01-18-2018
10:27 AM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-18-2018
12:34 PM

01-18-2018
12:34 PM

#set ( "The Most Loads<br />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 10^{308} + 4.9406564584124654 x 10^{324 }-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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-18-2018
01:51 PM

01-18-2018
01:51 PM

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!