Make a Document or Banner Come Up Randomly on Each Load of the Page

Mindwatering Incorporated

Author: Tripp W Black

Created: 03/13/2000 at 01:11 AM

 

Category:
Notes Developer Tips
Agents, Forms/Subforms

You can do this with a form or an Agent...

Form Solutions:
Option 1.
Computed field with a @DbColumn to a view getting the UNID of the documents you want to choose from (let's name it: unids)
... btw... How many?
(if there are to many documents this could be a little bit slow)

Option 2.
1. Create Field "RndNo"
Computed field that generates a random number between 1 and the number of elements of the first field.

2. Banner Computed for Display Field (or computed text) Where Banner to Be Displayed
code.txt

Agent Solution:
With an agent is the same logic except that you use LS, to get the view generated the randomnumber, then with this number use the view.GetNthDocument(rndno), from this document get UniversalID propertie then use the Print method to generate the JavaScript, the onLoad stuff applies in the sameway

Better Logic for Random:
from: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

VB:
Public Shared Sub Shuffle(ByVal arrays() As Integer)
Dim rng As New System.Random() ' i.e., System.Random
Dim n As Integer = arrays.Length ' the number of items left to shuffle (loop invariant)
While (n > 1)
Dim k As Integer = rng.Next(n) ' 0 <= k <= n-1 (0-based array)
n -= 1 ' n is now the last pertinent index;
Dim temp As Integer = arrays(n) ' swap array[k] with array[n]
arrays(n) = arrays(k)
arrays(k) = temp
End While
End Sub


Java:
public static void shuffle(int[] array)
{
Random rng = new Random(); // java.util.Random
// n is the number of items remaining to be shuffled.
for (int n = array.length; n > 1; n--)
{
// Pick a random element to swap with the nth element.
int k = rng.nextInt(n); // 0 <= k <= n-1 (0-based array)
// Swap array elements.
int tmp = array[k];
array[k] = array[n-1];
array[n-1] = tmp;
}
}

JavaScript:
function arrayShuffle(aArray) {
var mTemp,j,i=aArray.length;
while (i) {
j = ~~(Math.random() * i); // introduces modulo bias (see below),
// "~~" above will floor the number (which is faster than a function call to parseInt)
mTemp = aArray[--i];
aArray[i] = aArray[j];
aArray[j] = mTemp;
}
}



previous page