On Friday 11 May 2007 07:03 pm, Benjamin Slavin wrote:
> I've just uploaded a template filter called "several_random" to the
> django snippets site.
> http://www.djangosnippets.org/snippets/230/

The way you're picking indices is inefficient:

while len(picked_indices) < arg:
   candidate = random_module.randint(0, max_index) 
   if candidate not in picked_indices:
       picked_indices.append(candidate)

The loop may run many times, and potentially very many times if asked 
for a large number of items out a large list (it gets worse the 
larger the list as it blunders around trying to find one not chosen 
yet).  I think it would make more sense to use random.shuffle() and 
then simply take a slice of [:num_requested] of it.  Keep in mind 
that random.shuffle does its work in place (i.e. it does not return a 
shuffled copy of the list).  If you want to leave the original list 
alone, copy it first and then shuffle the copy.

Mordy

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to