Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomization 1

Status
Not open for further replies.

SPYDERIX

Technical User
Jan 11, 2002
1,899
CA
Hi,

I am currently working on a site for a newspaper and they want ads from their paper to show up along one of the sidebars and they have to be random from a list of many ads and there can't be duplicate ads.

I am storing all the image paths to the ads as well as urls to applicable sites in a seperate mysql db and want to know how I can randomly take say 4-5 ads out and place them in any order and not have them duplicate.

How do I go about doing something like this?

Thanks alot!

Nate

mainframe.gif

 
What about including a sequential number in each of your advert records in the database and keeping track of the lowest and highest serial numbers used in another record in the database.

Once you have those it should be a simple matter to generate a random number between the lowest and highest serial numbers used using the srand() and rand() functions.

srand((double)microtime()*1000000);
$random=rand($lowest,$highest);

As you generate each random number you could then place it in an array after checking that the number doesn't already exist inthe array. Once you've repeated the process 4 times (or more if required) you would be able to retrieve the database records based on the serial numbers in the array.


 
Hi,

Ok, I'm on the right track now, thanks.

But how do I check the current array if a number exists while it's in a for loop?

This is what I have so far:

[COLOR=003366]<?php

$highest = 30;
$numads = 5;

for ($i=0; $i < $numads; $i++)
{
srand((double)microtime()*1000000);
$random=rand(1,$highest);

$varar[$i] = $random;
}

for ($n=0; $n < $numads; $n++)
{
echo $varar[$n] . &quot;<BR>&quot;;
}

?>[/color]

Am I going about this the right way?

Thanks!

Nate

mainframe.gif

 
When you have a value to be loaded into the array you would enter a sub-loop checking the value to be inserted against each of the values already in the array in turn.

If the value to be inserted into the array is found in the array then you use the continue statement to skip the step and re-calculate the value rather than inserting it.

If the value to be inserted is not found then obviously you insert it into the array in the normal manner.

 
Or instead of using a sub-loop you could use the array_search() function to check.

ie. if(array_search($random,$varar)!=FALSE)

then the value in $random already exists in the array and should be re-calculated.

<?php

$highest = 30;
$numads = 5;

for ($i=0; $i < $numads; $i++)
{
srand((double)microtime()*1000000);
$random=rand(1,$highest);

if(array_search($random,$varar)==FALSE)
$varar[$i] = $random;
else
$i--;

}

for ($n=0; $n < $numads; $n++)
{
echo $varar[$n] . &quot;<BR>&quot;;
}

?>
 
newmangj,

Thanks alot, now they aren't repeating, however in addition to the vars being output I get an error:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in \random.php on line 11

I also get the same error using array_search ???

I can suppress that using an @ sign, but is there something else that should be done so that it doesn't error every time. I don't want to just hide the error if possible.

Thanks :)

Nate

mainframe.gif

 
nm, I got it.

The reason for the error is b/c there is no default set for the variable and when it does the in_array function, there is no actual array to search.

I just set $varar[] = null; before the loop and now there is no error.

Thanks so much,
star.gif
for you!

Nate

mainframe.gif

 
Glad to be of service. The other alternative is to only do the in_array check when the value of $i is greater than 0.

In this way the $varar array would be in existance (created when $i = 0) and you would avoid a superfluous call to in_array() when inserting the 1st value into the array (where it cannot be a duplicate by default!)

Cheers - Gavin
 
That method is actually slower. When I set varar[] to null before the first for loop it doesn't have to do as much cheking each time it cycles and it's faster, which is important to me.

Thanks again!

Nate

mainframe.gif

 
What about this

You break the exercise into 2 parts, the first part just calculates the random number and places it into slot 0 of the array. Then you run the loop but start $i at a value of 1.

<?php

$highest = 30;
$numads = 5;

srand((double)microtime()*1000000);
$random=rand(1,$highest);
$varar[0] = $random;

for ($i=1; $i < $numads; $i++)
{
srand((double)microtime()*1000000);
$random=rand(1,$highest);

if(in_array($random,$varar)==FALSE)
$varar[$i] = $random;
else
$i--;

}

for ($n=0; $n < $numads; $n++)
{
echo $varar[$n] . &quot;<BR>&quot;;
}

?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top