Hey Paul,
Thanx for responding.
Yeah, I read that too, but it didn't occur to me that I could use it in the place of array_push(). To be frank, I still don't see how I could use it to generate a range of numbers in an array.
Now before you think what I think you're going to think, I know there is the range() function, which does exactly what I was trying to do from the start. It, in conjunction with the shuffle() function worked great, just as stated in the PHP manual. The only problem is that when I uploaded it on to my web host, I got errors stating that range() and shuffle() were undefined! That's why I started trying these longhand methods, so to speak. The array_push() function sounded great, until I found that the server didn't like shuffle() either (I found this out after posting this message). Here is what I came up with. The echo comments are for testing purposes only.
...........................................
$randval;
$num[10];
$pos = 0;
$control = 10;
for( ; $pos < $control; )
{
// seed with microseconds since last "whole" second
mt_srand((double)microtime()*1000000);
// generate number
$randval = mt_rand(1, $control);
if ($pos == 0)
{
$num[0] = $randval;
++$pos;
echo $pos;
echo " ";
echo $randval; echo "<br>";
}
else if (in_array($randval, $num))
{
echo $randval; echo " is a duplicate";
echo "<br>";
}
else
{
$num[$pos] = $randval;
++$pos;
echo $pos;
echo " ";
echo $randval; echo "<br>";
}
}
echo "<br>";
echo "If this number is the same as the last in the list above, ";
echo "then the array is without a doubt created correctly. <br>";
print($num[9]);
............................................
I'm using this for random banner add control. I will probably keep this in the event that I encounter more servers that insist on coughing on the range() and shuffle() fuctions. I am planning on extending it's capabilities in the future to include tracking how much each add is displayed.
On another topic, do you have any idea why a PHP version that supports particular functions would consider them "undefined" when another has no issues?
Thanx again for the feedback and if this script helps you, have at it.
Later on,
Big Din K.R.
www.linuxrookies.net
?>