Forum Moderators: coopster
function test(){
$text = Array();
$text= "test1";
$text[2] = "test2";
$text[3] = "test3";
$text[4] = "test4";
$text[5] = "test5";
$text[6] = "test6";
$one = $text[rand(1,3)];
$two = $text[rand(1,3)];
$five = $text[rand(4,6)];
if ($one == $two)
echo $five;
else
echo $one;}
test();
?>
[1][edited by: Simone100 at 10:27 pm (utc) on Sep. 17, 2007]
one solution to your problem:
create a table with an autoincreased id, and store there your items. Make sure that these are also unique.
then use rand() with your ids from that table and then extract the corresponding values from the ids
function randomString($array){
shuffle($array);
return $array[0];
}
$text_array = array('test1','test2','test3','test4','test5','test6');
echo randomString($text_array);
Or do you want to show two or three items from that array without duplication on a single page?
function randomString(&$array, $destructive = false){
if(!is_array($array) ¦¦ count($array) <= 0) return false;
shuffle($array);
$return = $array[0];
if($destructive) unset($array[0]);
return $return;
}
$text_array = array('test1','test2','test3','test4','test5','test6');
echo randomString($text_array, true);
echo randomString($text_array, true);
echo randomString($text_array, true);
// print_r($text_array); // shows the remaining unused strings
I need to have one result only from all the lines in the array. One item only picked randomly, no matter how many lines there are. So just "test10" text or whatever else randomly is pulled.
If two items are picked twice in a row rarely is ok, like 1 in 30 times or less. I just don't want the same two items picked in a row often.
On this, its still picking two items in a row too often.
<?php
function randomString($array){
shuffle($array);
return $array[0];
}
$text_array = Array();
$text_array[1] = "test1";
$text_array[2] = "test2";
$text_array[3] = "test3";
$text_array[4] = "test4";
$text_array[5] = "test5";
$text_array[6] = "test6";
$text_array[7] = "test7";
$text_array[8] = "test8";
$text_array[9] = "test9";
$text_array[10] = "test10";
$text_array[11] = "test11";
$text_array[12] = "test12";
$text_array[13] = "test13";
$text_array[14] = "test14";
$text_array[15] = "test15";
echo randomString($text_array);?>
[1][edited by: Simone100 at 8:38 am (utc) on Sep. 19, 2007]
jatar don't know what you mean by this
"If you don't want repetition then only show a single item in a small set"
And an example of the table approach would help. Thank you.
[edited by: Simone100 at 9:37 am (utc) on Sep. 19, 2007]
the trouble is, it's random, if you want to control the output and give even exposure to all items then you will need to use more logic and track views etc. It is hard to make random functions conform to some type of pattern.
Anyone know why this picks items a LOT more randomly, and why it doesn't choose to pick the first 2? Once someone can tell me why it works the way it does, then I'll understand how to get it to choose all the numbers occassionally. Thanks a lot.
<?php
function randstring(){
list($usec, $sec) = explode(" ", microtime());
srand((int)($usec*10));
$text = Array();
$text[1] = "test1";
$text[2] = "test2";
$text[3] = "test3";
$text[4] = "test4";
$text[5] = "test5";
$text[6] = "test6";
$text[7] = "test7";
$text[8] = "test8";
$text[9] = "test9";
$text[10] = "test10";
$text[11] = "test11";
$text[12] = "test12";
$text[13] = "test13";
$text[14] = "test14";
$text[15] = "test15";
$rand_value = $text[rand(1,15)];
echo($rand_value."<br>");
}
randstring();
?>
What you want is "random without repeating" and the only way to do that is to create a list of the items, randomize it a few different ways, put it all together then just loop through your "random playlist." Thankfully, that's not too terribly complex. Example:
<?php
session_start();
$text = array();
$text[1] = "test1";
$text[2] = "test2";
$text[3] = "test3";
$text[4] = "test4";
$text[5] = "test5";
function resetRandom(){
global $text;
$random_iterations = 7; // if this is short, you'll get more repetition because repetition can occur whenever the playlist is reset
$text_keys = array_keys($text);
$_SESSION['random'] = array();
$_SESSION['randomInc'] = 0;
for($i=0; $i<$random_iterations; $i++){
shuffle($text_keys);
if(count($_SESSION['random']) >= 1 ){
while($text_keys[0] == $_SESSION['random'][count($_SESSION['random'])-1]){ // make sure we don't end up with duplication within our master playlist
shuffle($text_keys);
}
}
$_SESSION['random'] = array_merge($_SESSION['random'], $text_keys); // 'concatenate' the arrays together
}
}
function echoRandom(){
global $text;
if(!isset($_SESSION['random']) ¦¦!isset($_SESSION['randomInc'])){
resetRandom(); // first time visitor
}
$_SESSION['randomInc']++;
if(!isset($text[$_SESSION['random'][$_SESSION['randomInc']]])){ resetRandom(); }
return $text[$_SESSION['random'][$_SESSION['randomInc']]];
}
echo '<h1>'.echoRandom().'</h1>';
echo '<pre>'; print_r($_SESSION['random']); echo '</pre>';
?>
If someone can tell me why this following one works without cookies I'd appreciate it. I'm also looking up its elements to see if I can figure it out.
[1]
<?php
function randstring(){
list($usec, $sec) = explode(" ", microtime());
srand((int)($usec*10));
$text = Array();
$text[1] = "test1";
$text[2] = "test2";
$text[3] = "test3";
$text[4] = "test4";
$text[5] = "test5";
$text[6] = "test6";
$text[7] = "test7";
$text[8] = "test8";
$text[9] = "test9";
$text[10] = "test10";
$text[11] = "test11";
$text[12] = "test12";
$text[13] = "test13";
$text[14] = "test14";
$text[15] = "test15";
$rand_value = $text[rand(1,15)];
echo($rand_value."<br>");
}
randstring();
?>
It's like the problem of flipping a coin. You can flip a coin ten times in a row and actually get ten heads. It's only when you flip a coin thousands of times will the heads/tails ratio approach 50/50.
Now what are the odds of rolling the same number twice in a row using a six-sided die? Much lower than the odds of getting two heads in a coin flip in a row. Simply because there are more choices.
You'll always have repeats unless you "stack the deck" (so to speak) to not have repeats. And... If you want to make sure that a particular user doesn't see the same thing twice in a row you'll need to do some kind of user tracking. Otherwise, you can prepare a single server side "random" list but since users will come in simultaneously, drawing from the same list means any particular user might end up pulling the same thing twice.
Non-repeating random order: 1, 2, 1, 3, 2, 3, 1, 2
User A gets item #1
User B gets item #2
User A gets item #1 <- repeat
User C gets item #3
User B gets item #2 <- repeat
User C gets item #3 <- repeat
Of course, you can always fall back to a server side or simple random if the user doesn't have cookies enabled. In which case that user will end up with repeats... no way to avoid that completely.