Forum Moderators: open

Message Too Old, No Replies

random loop through JS Array?

loop javascript array

         

quiksan

7:37 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



ok, so I'm not a JS hero by any stretch, and I'm stuck here. hoping someone can help me out a bit.

I need to display a dozen or so banners in a random order each time the page is loaded, WITHOUT DUPLICATING OR LEAVING-OUT ANY.

WHAT I'VE DONE:
built an array containing the URL and IMG file path.
the banner with its URL is displayed on the page.
it randomly loops through this action until length of the array is met.

problem is, it duplicates the same banner, and leaves a lot out.
Can anyone help?!?! I've gotten this far, I just need to get over tha hump.

Thank you, thank you, thank you!
here's the code:


<html>
<body>
<script type="text/javascript">

function banner(file, url) {
this.file = file
this.url = url
}
var ckpBanners = new Array()
ckpBanners[0] = new banner("http://cruisinkidspages.com/!/images/banners/MOMS_banner.gif", "http://www.mothersofmultiples.com/")
ckpBanners[1] = new banner("http://cruisinkidspages.com/!/images/banners/jplizzy_banner.gif", "http://www.jplizzy.com/")
ckpBanners[2] = new banner("http://cruisinkidspages.com/!/images/banners/juiceplus_banner.gif", "http://www.wholefoodprevention.com/")
ckpBanners[3] = new banner("http://cruisinkidspages.com/!/images/banners/panera_banner.gif", "http://www.paneracolorado.com/")
ckpBanners[4] = new banner("http://cruisinkidspages.com/!/images/banners/joeyds_banner.gif", "http://www.joeydfoods.com/")
ckpBanners[5] = new banner("http://cruisinkidspages.com/!/images/banners/4littlemonkeys_banner.gif", "http://www.fourlittlemonkeys.com/")
ckpBanners[6] = new banner("http://cruisinkidspages.com/!/images/banners/kidzart_banner.gif", "http://www.kidzart.com/")
ckpBanners[7] = new banner("http://cruisinkidspages.com/!/images/banners/mtn_banner.gif", "http://www.mtnhighcleaners.com/")
ckpBanners[8] = new banner("http://cruisinkidspages.com/!/images/banners/pamperedchef_banner.gif", "http://www.pamperedchef.biz/barbsbites/")
ckpBanners[9] = new banner("http://cruisinkidspages.com/!/images/banners/nannyconnection_banner.gif", "http://www.nannyconnectionusa.com/")
ckpBanners[10] = new banner("http://cruisinkidspages.com/!/images/banners/monkeytoes_banner.jpg", "http://www.monkey-toes.com/")
ckpBanners[11] = new banner("http://cruisinkidspages.com/!/images/banners/slhome_banner.gif", "http://www.southernlivingathome.com/athomewithdebbie")
ckpBanners[12] = new banner("http://cruisinkidspages.com/!/images/banners/icecream_banner.gif", "http://www.sundaeisland.com/")
ckpBanners[13] = new banner("http://cruisinkidspages.com/!/images/banners/hcn_banner.gif", "http://www.highcountrynannies.com/")
ckpBanners[14] = new banner("http://cruisinkidspages.com/!/images/banners/realtor_banner.gif", "mailto:hender.adams@homesincolorado.com")
ckpBanners[15] = new banner("http://cruisinkidspages.com/!/images/banners/wahpa.gif", "http://hometown.aol.com/athomepromotions/WAHPC.html")
ckpBanners[16] = new banner("http://cruisinkidspages.com/!/images/banners/yellowpgs_banner.gif", "http://www.yellowpages.com/SearchResult.aspx?page=home&searchform=homesearch&typeorname=type&search=recreation&searchfor=recreation&method=and&city=denver&state=CO")

for (i = 0; i <= ckpBanners.length-1; i++) {
function get_random() {
var ranNum = Math.round( Math.random() * 4 );
return ranNum;
}
var counter = get_random();
document.writeln('<A HREF="'+eval('ckpBanners[counter].url')+'"><IMG SRC="'+eval('ckpBanners[counter].file')+'" border="0"></A><P />');
}

</script>
</body>
</html>

quiksan

9:02 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



^^^
bump

Lord Majestic

9:17 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var ranNum = Math.round( Math.random() * 4 );

You need to multiply by number of banners in the array less 1 (ie 16), not 4 as otherwise you will get integers from 0 to 4 thus showing one of the first 5 banners. With total number of shown banners being greater than 5, repeatitions are inevitable.

Also you should consider that a series of random numbers from 0 to N does not guarantee that you will get unique numbers, ie non-repeatable. Ratio of repeatition will depend on quality of (preudo)random generator and number of digits to choose from.

quiksan

9:45 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



I had actually just figured that out (except I used the ckpBanners.length so I don't have to remember to change it as the list expands/contracts.

wasn't sure I understood how to deal with the UNIQUE numbers issue. can you expand on "Ratio of repeatition will depend on quality of (preudo)random generator and number of digits to choose from." a bit?

thanks. i appreciate the help

Lord Majestic

9:49 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Some people equalise random with unique, ie when they generate X random numbers they think that all of them will be unique. This may or may not happen, but the series of numbers you get are certainly not guaranteed to be unique because there should be a fair random chance of each of the numbers you get to reappear in the series again.

In your case non-uniqueness would mean that some of the banners would be repeated. Just don't count on random numbers to be unique and you will be fine.

quiksan

1:38 pm on Aug 11, 2004 (gmt 0)

10+ Year Member



ok, yeah, i got that. Sorry, i thought you were telling me how to wokr against that and I just wasn't following.

I'm rethinking this a little, and realize that my best bet would be to randomly(uniquely) reorder the array, and then iterate through it to print it to the page.

I did a bunch of google searching, but couldn't find anything definitive on randomly resorting an array.
Anyone got some pointers on this?