Page 1 of 1

Random Variables

Posted: Mon Aug 05, 2002 10:51 pm
by Boz
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. :cry:

Posted: Tue Aug 06, 2002 1:49 am
by twigletmac
If you post a bit of your code (not all of it just the bit that does the sorting and the picking out of random variables) then we might be able to point you in the right direction. We can offer suggestions but they may just make things worse if we can't see the code.

Mac

Random without repeptition

Posted: Tue Aug 06, 2002 9:20 am
by AVATAr
With an array you could do this:

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++)
      &#123;
       $rand = rand(0,$limit);
       echo $array&#1111;$rand];
       echo "<br>";

       //-- swap last and array(rand)
       $aux = $array&#1111;$limit];
       $arrat&#1111;$limit] = $array&#1111;$rand];
       $array&#1111;$rand]= $aux;
       //-- end swap

       $limit = $limit -1;
      &#125;

 echo $array&#1111;0];
?>
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

Posted: Tue Aug 06, 2002 9:28 am
by hob_goblin
eh do you mean something like this?

Code: Select all

function random_var()&#123;
$num = function_num_args();
srand((double)microtime()*1000000);
$ran = rand(0, $num);
$var = function_get_arg($ran);
return $var;
&#125;

echo random_var("You", "can", "put", "as", "many", "args", "as", "you", "want");

Posted: Tue Aug 06, 2002 9:30 am
by volka
it's easier than you think ;)

Code: Select all

$array = array(1,2,3,4,5);
srand ((float)microtime());
shuffle($array);
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

Posted: Tue Aug 06, 2002 9:33 am
by hob_goblin

Code: Select all

srand ((float)microtime());
echo array_rand($_POST);
;)

I think

Posted: Tue Aug 06, 2002 9:34 am
by AVATAr
I think the main problem of Boss is:
well, i can get the variables to display randomly, but I cant get it to not repeat the same variable twice
so ... random search in an array is not an isue for him.

:wink:

Posted: Tue Aug 06, 2002 9:41 am
by volka
maybe this ;)

Code: Select all

srand((integer)md5(microtime()));
shuffle($array);
foreach($array as $var)
   print($var.'<br/>');

Posted: Tue Aug 06, 2002 9:45 am
by hob_goblin
oh, well the php manual comment say that shuffle() is poor.

Code: Select all

function array_rand_elim (&$array1, $array2 = array ())
&#123;
	if (!count ($array1))
		return array1;

	srand ((double) microtime() * 10000000);
	$rand = array_rand ($array1);

	$array2&#1111;] = $array1&#1111;$rand];
	unset ($array1&#1111;$rand]);

	return $array2 + array_rand_elim ($array1, $array2);
&#125;

$array = array_rand_elim ($array);
was in one of the comments..