Random Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Boz
Forum Newbie
Posts: 1
Joined: Mon Aug 05, 2002 10:51 pm

Random Variables

Post 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:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Random without repeptition

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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");
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

srand ((float)microtime());
echo array_rand($_POST);
;)
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

I think

Post 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:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe this ;)

Code: Select all

srand((integer)md5(microtime()));
shuffle($array);
foreach($array as $var)
   print($var.'<br/>');
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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..
Post Reply