3

Here's the code I'm using:

<?php
$input = array("Test1", "Test1", "Test1", "Test1","Test2");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]];
?>

Bascially I want to echo Test1 and Test2 randomly - BUT I want Test1 to show up 4x more than Test2 does.

I'm guessing that by adding it in 4 times more than Test2, it should show up 4 times more - is that code correct or should I be doing it a different way?

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
user2410854
  • 139
  • 2
  • 8

5 Answers5

0

I'm guessing that by adding it in 4 times more than Test2, it should show up 4 times more

This approach looks correct. Let's test it:

$input = array("Test1", "Test1", "Test1", "Test1", "Test2");

for ($i=0; $i < 100000; $i++) { 
    $rand_key = array_rand($input, 1);
    $result[] = $input[$rand_key];
}

$values = array_count_values($result);
print_r($values);

The output I got was:

Array
(
    [Test1] => 80123
    [Test2] => 19877
)

It's 4.03094028274 times!

Although, in your example, you always echo the first element in the $input array, so it's not random at all. You need to access the $random_keys index instead:

$rand_key = array_rand($input, 1);
echo $input[$rand_key];

Note that I've changed the array_rand() statement so it only receives one value at a time.

This can also be done using shuffle():

$rand_key = shuffle($input);
echo $input[0];
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

Yes, by having "Test1" 4times and "Test2" 1time in the array chance of "Test1" being displayed is 4/5 and "Test2" is 1/5. Meaning if you run it for 100 times you will get 80 Test1 and 20 Test2.

Take a look at this question as well:

Generating random results by weight in PHP?

Community
  • 1
  • 1
Mojtaba
  • 6,012
  • 4
  • 26
  • 40
0

If we modify your code slightly to run in a loop:

$input = array("Test1", "Test1", "Test1", "Test1", "Test2");
$results = array("Test1" => 0, "Test2" => 0);

for ($i = 0; $i < 1000; $i++) {
    $rand_keys = array_rand($input);
    $results[$input[$rand_keys]]++;
}

var_export($results);

I get results that look like this:

array (
  'Test1' => 807,
  'Test2' => 193,
)

That looks pretty close to four times as much to me!

Note that I have changed your array_rand call so it only receives one result at a time, because otherwise look at what happens:

$rand_keys = array_rand($input, 2); // get two results 
echo $input[$rand_keys[0]];         // echo the first one

The results appear in the order they are in in the original array. In this case, that means that Test2, when it appears, will always be last, so will never show up in your results.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

If it's just the two values, you could do this much more efficiently by just randomly selecting amongst them using php's rand() and weighting the results to correspond to your desired output.

You want "Test1" 4/5 of the time and "Test2" 1/5 of the time you could do this easily like this:

echo (rand() > (getrandmax() / 5)) ? "Test1" : "Test2";

You can verify this does the right thing with a loop, as many of the other answers have done.

for ($ii = 0; $ii < 10000; ++$ii)
  $r[(rand() > (getrandmax() / 5)) ? "Test1" : "Test2"]++; 
var_export($r);

outputs

array (
  'Test1' => 8027,
  'Test2' => 1973,
)
Dan
  • 4,312
  • 16
  • 28
0

Your code will always pick in the ratio of 4:1. For 5 chances, you're guaranteed to get 4 of "1" and 1 of "2". Is that really what you want? That's not the same thing as a 4:1 ratio over many tries.

You could have an array("Test1", "Test2"). Pick an index between 0 and 4 inclusive (equal chance of any one of the five). If 0 to 3 (less than 4), pick $input[0]. Otherwise (4), pick $input[1].

Phil Perry
  • 2,126
  • 14
  • 18