0

I using the rand from php. That below script is working perfect, But My question is to give the priority for the one value in array(Show multi times). For example 100 times it randomly show the value, Is it possible to make the 0 to 99 times it echo the dofollow and 1 time it show nofollow using rand

$input_nofollow = array("nofollow", "dofollow");
$random = rand(0, 1);
echo $input_nofollow[$random];
2
  • Sorry what ? Do you want a simple for loop ? Commented Jul 17, 2013 at 13:08
  • 2
    if you have this much LOGIC, then this cant be a RANDOM one! Commented Jul 17, 2013 at 13:13

3 Answers 3

3
$random = rand(0, 100);
$selected_key = 0;
if($random < 90) $selected_key = 1;
else $selected_key = 0;

echo $input_nofollow[$selected_key];

This give you a ~90% chance to get the index 1.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Thanks for your awesome response only change i made is $random<=90 then only it returns dofollow
yeah, my mistake. I have changed the order.
1

Sounds like you're looking for a shuffle, not a random generator. Create an array with 99 "a"s and one "b", shuffle it, output in order.

Alternatively generate a random number between 1 and 100, loop with a counter, if the counter equals the random number output "b", else "a".

Comments

1

Try Something Like This:

<?php
    $input_nofollow = array("nofollow", "dofollow");

    $random = rand(0, 100);
    $i = 0;
    while (1 /* Your Condition */ )
        if (++$i > 100) {
            $i = 0;
            $random = rand(0, 100);
        }

        if ($i == $random)
            echo $input_nofollow[0];
        else
            echo $input_nofollow[1];
    }
?>

Comments

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.