1

I was wondering how I can make it to where 3 variables never have the same number using rand()?

<?php
$myAd[1] = '<a href="http://www.pegor.com">Free PHP Tutorials</a>';
$myAd[2] = '<a href="http://www.lifestinks.info">Fast Facebook Proxy</a>';
$myAd[3] = '<a href="http://www.mozilla.org">Fastest and Secure Web Browser</a>';

$adId = rand(1,count($myAd));
$adId2 = rand(1,count($myAd));
$adId3 = rand(1,count($myAd));
if ($adId === $adId2) { $adId = ($adId2 - 1) }
if ($adId === $adId3) { $adId = ($adId3 - 1) }
if ($adId2 === $adId3) { $adId2 = ($adId3 - 1) }

echo $myAd[$adId];
echo '<br>';
echo $myAd[$adId2];
echo '<br>';
echo $myAd[$adId3];
?>

In a nutshell what I would like to accomplish is my site displaying 3 products on the left column. Every time a user refreshes they change. The issue I keep running into is 2 of the variables that the random number generates are the same. How can I make it to where they always land on different numbers so 3 different products are displayed at a time instead of 2 the same and 1 different? Note: I will be adding more items to the array, that is just a test script until I figure it out.

6
  • 1
    Check out link and list() construction. Or shuffle(). Commented Apr 26, 2013 at 8:21
  • Hmm I honestly do not understand it. My code works if I remove all the if statements. The only issue is sometimes 2 of the displayed items will be the same. Is there not a possibility of saying if this variable equals this variable subtract or add this? Commented Apr 26, 2013 at 8:25
  • The main problem with your code is that sometimes (actually, something like a 2 in 3 chance) you will be deducting 1 from 1, which will mean 0, and there is nothing in $myAd[0]. So it will error. Commented Apr 26, 2013 at 8:40
  • I added one at 0 because I caught my error but I did accept CORRUPTs answer since it worked for what I needed. Especially since they used the foreach function. I planned on placing each product in a seperate div but with foreach I can easily let it create the divs for me for each product. Thanks for the help anyway though :) Commented Apr 26, 2013 at 8:43
  • Indeed, the secondary problem was that "If variable equals variable change a variable" is an inefficient way to pick three different values. You now have chosen a solution that elegantly solves your problem but doesn't actually answer your title question. Also, you did not need to add "Resolved" to your title. Commented Apr 26, 2013 at 8:47

3 Answers 3

1

Try shuffle() and array_slice():

$myAd = array();
$myAd[1] = '<a href="http://www.pegor.com">Free PHP Tutorials</a>';
$myAd[2] = '<a href="http://www.lifestinks.info">Fast Facebook Proxy</a>';
$myAd[3] = '<a href="http://www.mozilla.org">Fastest and Secure Web Browser</a>';
// more of...

shuffle($myAd);

$myAd = array_slice($myAd, 0, 3);

foreach($myAd as $value){
    echo $value, '<br/>', PHP_EOL;
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

But if I have 50 items in the array won't it echo all 50 items?
@VampireNews Then, array_slice() helps.
1
$adId = rand(1, count($myAd));
do
    { $adId2 = rand(1, count($myAd)); }
while ($adId2 == $adId);

do
    { $adId3 = rand(1, count($myAd)); }
while ($adId3 == $adId || $adId3 == $adId);

But better to use shuffle() and array_slice().

Comments

0

Example how to compare two but you can improve of basically you need most

$rnd1 = rand(0,9);
do {
  $rnd2 = rand(0,9);
} while ($rnd1 == $rnd2);

Firstly you need to put your random variable to one variable and then do while where the random 1 equals to random 2 you can do more complex from what you need.

GBU

2 Comments

Of course, when the numbers are substituted for the range of adverts, a sanity check is needed to ensure there actually are enough adverts otherwise there would be an infinite loop.
Thanks @PaulGregory for the explanation though it was a simple explanation :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.