edit: well, i can get the variables to display randomly, but I cant get it to not repeat the same variable twice.
Random Variables
Moderator: General Moderators
Random Variables
I'm trying to write a script so that it lists the text variables which were submitted in a form, in a random order. So far, I have had no luck. All I've managed to do is make sure they fill in all the forms. Please help!
edit: well, i can get the variables to display randomly, but I cant get it to not repeat the same variable twice.
edit: well, i can get the variables to display randomly, but I cant get it to not repeat the same variable twice.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
Random without repeptition
With an array you could do this:
The idea is to randomly select the position of the array and swap that position with de last one in the array and then make another random search but changing the limit of the array so you dont use de last one.
Sorry my english
Code: Select all
<?php
//display content of an array randomly without repetition
$array = array(1,2,3,4,5);
$limit = 4; //Length of the array
for ($i = 0; $i < 4; $i++)
{
$rand = rand(0,$limit);
echo $arrayї$rand];
echo "<br>";
//-- swap last and array(rand)
$aux = $arrayї$limit];
$arratї$limit] = $arrayї$rand];
$arrayї$rand]= $aux;
//-- end swap
$limit = $limit -1;
}
echo $arrayї0];
?>Sorry my english
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
eh do you mean something like this?
Code: Select all
function random_var(){
$num = function_num_args();
srand((double)microtime()*1000000);
$ran = rand(0, $num);
$var = function_get_arg($ran);
return $var;
}
echo random_var("You", "can", "put", "as", "many", "args", "as", "you", "want");it's easier than you think
But there have been some complains about shuffle in the user note (haven't experienced that)
You might also want to take a look at array_rand
Code: Select all
$array = array(1,2,3,4,5);
srand ((float)microtime());
shuffle($array);You might also want to take a look at array_rand
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Code: Select all
srand ((float)microtime());
echo array_rand($_POST);- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
I think
I think the main problem of Boss is:

so ... random search in an array is not an isue for him.well, i can get the variables to display randomly, but I cant get it to not repeat the same variable twice
maybe this 
Code: Select all
srand((integer)md5(microtime()));
shuffle($array);
foreach($array as $var)
print($var.'<br/>');- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
oh, well the php manual comment say that shuffle() is poor.
was in one of the comments..
Code: Select all
function array_rand_elim (&$array1, $array2 = array ())
{
if (!count ($array1))
return array1;
srand ((double) microtime() * 10000000);
$rand = array_rand ($array1);
$array2ї] = $array1ї$rand];
unset ($array1ї$rand]);
return $array2 + array_rand_elim ($array1, $array2);
}
$array = array_rand_elim ($array);