2

So I am looking to alternate between 5 different variables but want the 'load' to be equal on each variable. I have seen it done with just two variables and alternating based on random number generated withe limits being set. But trying to expand that idea on to five variables. Thanks in advance.

Just for example purposes say I had 5 images and wanted each image to be shown equally after 100 tests. This is the easiest way I can think of explaining it.

This is how I did it with only 2 variables but trying to think about how to do it with more.

$ad1 = '<img src... >';
$ad2 = '<img src...2 >';

echo mt_rand(0, 1) ? $ad1 : $ad2;

Source:PHP Switching between two variables equally without using database

So this is what I have going just want to know what the ideas on equally being between the different variables.

$input = array("a", "b", "c", "d");
$rand_keys = array_rand($input);
echo 'var key ="'.$input[$rand_keys].'"';
Community
  • 1
  • 1
Fogolicious
  • 412
  • 8
  • 22
  • It's not clear what you're trying to accomplish. Could you post some sample code and update your question with more detailed explanation? – Amal Murali Oct 07 '13 at 21:31
  • 2
    Have you tried `counter % 5` where `counter` increases with each visit? – David Starkey Oct 07 '13 at 21:31
  • This is called "learning the language", "arrays" and "[`rand()`](http://php.net/manual/en/function.rand.php)" – Bojangles Oct 07 '13 at 21:31
  • @Bojangles That doesn't really guarantee equality – David Starkey Oct 07 '13 at 21:33
  • 1
    Do you want to show each image an equal number of times or do you want a random distribution? These are two different things although they might give the same result some times. – Sébastien Oct 07 '13 at 21:34
  • I updated with an example that I went off from before when there was only 2 variables and in the long run with the law of big numbers it would be 50/50 but I do want it to be random from the 5 but realize it is really only equal in the idea of big numbers. – Fogolicious Oct 07 '13 at 21:42
  • @Fogolicious: `$which = mt_rand(0, $num_options - 1);`. If you have many options, consider using an array, so you could use `$arr[$which]` to give you your choice. – Murilo Vasconcelos Oct 07 '13 at 21:52
  • Edit to add the solution I thought of. Advice on this? – Fogolicious Oct 07 '13 at 21:55
  • @fogolicious did you consider my first solution? Do they need to be randomized or do you just want them to rotate equally? – Kenny Oct 07 '13 at 21:57

3 Answers3

3

Two options below, because your question is titled "swithcing between 5 variables equally" - There is an equal distribution solution and a randomized solution.

Notice you need apc installed for the equal rotation solution to work. It is a fairly common php module and can be installed quite easily if you have permission to do so:

Redhat/centos : yum install php-apc

Ubuntu: sudo apt-get php-apc

If you are using shared hosting and they don't have apc installed then this won't work for you :(

<?php

//If you want perfectly equal distribution.
$variables = array('a', 'b', 'c', 'd', 'e');
if (!($this_var = apc_fetch('last'))) $this_var = 0;
echo $variables[$this_var];
$this_var++;
$this_var = $this_var % 5;
apc_store('last', $this_var);


//If you want random distribution
$variables = array('a', 'b', 'c', 'd', 'e');
$index = mt_rand(0, 4);
echo $variables[$index];
Kenny
  • 1,543
  • 9
  • 16
  • apc version works perfectly but how would I integrate result of the mysql query into it so that `$variables =` several rows from a table? – inside83 Apr 09 '21 at 17:42
2

If you want a random pick out of a set you can use array_rand().

$images = array( '<img src="img1.png">', '<img src="img2.png">', '<img src="img3.png">', '<img src="img4.png">', '<img src="img5.png">' );

$pick = array_rand( $images );

$pick will be a random key from the $images array.

This is easily extensible as you can keep adding elements to the array. Here I am of course following your example of adding the <img> tag to the array but you could just as easily add only some identifier to the array.

Sébastien
  • 11,860
  • 11
  • 58
  • 78
  • Deleted mine and up voted yours, I always forget about all the `array_*` functions ;) – SamV Oct 07 '13 at 21:50
  • This is what I basically went with. Edited my question, check that code and let me know if you would change the code I have in anyway. – Fogolicious Oct 07 '13 at 21:57
  • 1
    @Fogolicious if you only want one element you don't need the second parameter in `$rand_keys = array_rand($input, 2);` What you are doing with that returns an array with 2 keys. Without the second param the function returns only one key (not in an array). So if you understand that, then you should be OK. – Sébastien Oct 07 '13 at 22:03
  • @Sébastien Thanks I didn't notice I left that in there from the example. – Fogolicious Oct 08 '13 at 13:25
0

I would suggest using a Class to handle each "object":

<?php
  ...
  ...
  class RandObjClass
  {
     var $myCounter = 1;
     var $mySeed = time();
     var $myReference = "...path-to-image-file..."; # Image for example
     function getWeight() {
        srand($mySeed);
        $myWeight = $this->myCounter * rand();
        return($myWeight);
     }
     setSeed($xSeed) {
        $this->mySeed = $xSeed;
     }
  }
...
...

Fill an array of x "RandObjClass" objects and use the "getWeight" to select the next one of "highest" weight.

You can play with affecting the weight via various methods, like counting. This can influence which is chosen next.

Alan Penny
  • 182
  • 1
  • 5