[Zope] Creating Document with random num as ID? PartII

Pavlos Christoforou pavlos@gaaros.msrc.sunysb.edu
Sat, 17 Apr 1999 18:05:17 -0400 (EDT)


Craig

Related to your question, the following external method is very handy:

import whrandom

def selection_nr(self,elems,no=15):
    '''selection_nr(self,no=15)

    returns a random selection of n elements from list elems without
    replacement'''

    if len(elems)<=15:
        return elems

    random_list=[]
    for i in range(no):
        elem=whrandom.choice(elems)
        random_list.append(elem)
        elems.remove(elem)
    return random_list

given a list of objects it will return a random list of length no.

So if you have a folder of images or quotes or whatever and you need to
return a random selection you can use the following DTML:

<!--# in "selection_nr(objectValues(['Folder'],4))"
<!--# var title -->
<!--#/in-->

will select randomly 4 subfolders and display their title.

Pavlos