A MODX Revolution Snippet demonstrating a very simple and effect way to randomize the insertion of a Chunk utilizing a prefix and number scheme: random1, banner23, header17.

Banners, customer endorsements, featured articles, call outs, and random quotes are just a few of the web based applications which can benefit from randomization.

PHP Source Code

<?php

/**
 * File randomChunk.php (requires MODx Revolution 2.1+)
 * Created on: 10/13/11 at 7:02 PM
 * Updated on: 05/06/2015 at 14:43.
 * Project shawn_wilkerson
 * @version 2.0
 * @category
 * @author W. Shawn Wilkerson
 * @link http://www.shawnWilkerson.com
 * @copyright Copyright (c) 2011, Sanity LLC. All rights reserved.
 * @license GPL 2
 *
 * Version 2 Limited the scope of the incoming variables to allow the parser to better randomize multiple calls in the same page.
 */
$out = false;
$prefix = $modx->getOption('chunkPrefix', $scriptProperties, 'random');
$chunks = $modx->getOption('totalChunks', $scriptProperties, intval(0));

if ($chunks > 0) {
    $chunk = $modx->getObject('modChunk', array(
        'name' => $prefix . rand(1, ($chunks)),
    ));
}
if (is_object($chunk)) {
    $out = $chunk->process();
}
return $out;

Example Snippet Calls

[[!randomChunk?chunkPrefix=`banner`&totalChunks=`4`]] -- user selectable prefix
[[!randomChunk?&totalChunks=`29`]] -- defaults to random as the prefix

Example Output

World Wide Stationery Manufacturing located in Hong Kong and Mainland China; ... were needing to brand our Company name and products... through a web site. Shawn [continues] to find new ways to present our products and Brand our name, as we gain recognition within the Bindery Industry [as the world's largest supplier of ring metals].

Russ Truelove, Representative
World Wide Stationery Manufacturing Group

Shawn is dependable, flexible, trustworthy, and professional. He takes pride in his work and his cost if very affordable. I highly recommend Mr. Wilkerson for any job...

Tony Barhoo, Pastor
Living Faith World Ministries

Comments

Like most randomization code, this relies on a higher number of items to choose from to produce a more noticeable random effect. With fewer modChunks to choose from, it becomes no different than the predictability of flipping coins for heads and tails.

I also created a sub Category to minimize clutter in the Element Tree. This can also become the foundation of eventually implementing a random MODX Revolution Snippet centered on categories, where only the category name is required in the Snippet call.

Also notice, I force the direct process of the modChunk before its being sent to the page.

MODX 2.3+

MODX 2.1 - 2.2