Is there a better way to write this short code? It goes to topics that dont exist, I want it to check first so that dosent happen, and not to go to topics in a particular forum, a forum url looks like this (last number being the forum id)
http://inny.ipbfree.com/index.php?showforum=1

<center><script>
//rough estimate of topics on your forum
topiccount = 2000
topicnum = Math.floor(Math.random()*topiccount)
document.write("<a href='http://inny.ipbfree.com/index.php?showtopic=" + topicnum + "'><img src=http://www.parexcellence.co.uk/weblink/images/randomlink.gif border=0></a>")
</script>
</center>

any ideas?

Try to change the random generation to an expression like:

topicnum = Math.round(Math.random()*(topiccount-1))+0;

Thanks But it still goes to non existant links.
Is there some way to make it check first that the topic exist?

Are you sure that you have 2000 topics? Or is that only the maximum number?
You would need to know the maximum number of topics for the random generation number to work between the range. So, in the expression:

topicnum = Math.round(Math.random()*(topiccount-1))+0;

"topiccount" must be the maximum o available topics.

I have just uinder 2000 topics but that changes everyday. It may go to something like topic number 1876 which may have been deleted or may not exist yet. This will trigger my error page. Id rather that didnt happen because it gives the impression that something is broken

So if I understand correctly, there is not a problem with the random generation but with the links; those, you have to include some kind of check before you write the link on your page, something like
...
get the number
check for existence
go back to check if does not exists
write the link
...

do you hold a record of existing/active topics?

I have Logs of Existing Topics yes..
the javascript for it (search stats) seems to be (url removed for security reasons)

<script type='text/javascript'>
 function dojump()
				
 {
				 	
if ( document.jumpmenu.val.options[document.jumpmenu.val.selectedIndex].value != '' )
				 	
{
				 		
window.location.href = 'url removed'+ '&' + document.jumpmenu.val.options[document.jumpmenu.val.selectedIndex].value;
				 	}
				 }
				 </script>

if "selectedIndex" within your function is the topic than can exist or not, I would create a function to find out the correct random destination, something like:

function destination() {

var urltogo = '';

while (urltogo == '')
{
selectedIndex = Math.round(Math.random()*(topiccount-1))+0;
urltogo = document.jumpmenu.val.options[document.jumpmenu.val.selectedIndex].value;
}

return urltogo;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.