Randomly pulling three different "posts" from an XML file.

3 views
Skip to first unread message

johnhutch

unread,
Jun 19, 2010, 6:40:29 PM6/19/10
to ColdFusion
hey guys. So I have a more general programming conundrum. I'm loading
several items from an XML file. The'yre loaded into a hash where each
item has a category -- so xml_hash[i][j] would refer to the j post in
i category.

I need to spit out three random items.

This client has a particularly slow server, so it's become a question
on how to do this as efficiently as possible. Obviously, I don't want
it to be possible to spit out the same item more than once.

The two ways I can think of doing this is:

1. Restructure the hash into an array of just xml_array[j] that
contains all items, dropping or relocating the i category. Generate a
rand number from 1 to arraysize and display that item. I then delete
that element and repeat two more times.

2. Keep the original xml_hash[i][j] and generate two random numbers:
1 to the number of categories and 1 to the number of posts in a given
category. THat part's easy. It gets tricky when we start removing
posts from categories and -- if the category then becomes empty,
removing the category.

I'm wondering if anyone can think of a better algorithm. Psuedo-code
appreciated. My coldfusion is a bit weak, so by all means, if the
language has any shortcuts, lemme know! Thanks!
Reply all
Reply to author
Forward
0 new messages