Page 1 of 1

arrays and random number generators.

Posted: Fri Feb 04, 2011 10:15 am
by shehan31
Hi all;
I am trying to work out to put the contents of a variable into an array(string array) and to generate a random number from it.
Ex : if i type "ABCA " as the input in the message box the out put has to be "BCDB". But the output shows as BBCD(looks liketo an alphebatical order).
I tried to eleminate that by assinging the contents of the value of the variable $value into an array and they 'krsort 'it. It gives me warnings.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Warning: krsort() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 16
B
Warning: array_rand() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 19

Warning: krsort() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 16
B
Warning: array_rand() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 19

Warning: krsort() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 16
C
Warning: array_rand() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 19

Warning: krsort() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 16
D
Warning: array_rand() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 19
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

<?php
if (isset($_POST['submit1']))
   { 
   $message = $_POST['Message'];
//$array1=array('A','C','B','A');
	$str= str_split($message);
	
	$array = array('A'=>'B','B'=>'C','C'=>'D');

foreach($array as $key=>$value){
	for($i=0;$i<count($str);$i++){
		if($key==$str[$i]){
	   //assigns the contents of the value variable into an array. 
		 $value==array();
		 
		 krsort($value);
		 echo"$value"; 
		 //generates a random number;
		 $random = array_rand($value);
		 echo"$random";
    
		}
		
	}
}
}

   
echo"

 <form action='Codelobster1.php' method='POST' >
 <td>
   <tr>
        <td>
		
         <font SIZE = 5 FACE = times new roman >Message</font>
        </td>
        <td>
        <input type='text' name='Message' maxlength='30'>
        </td>
   </tr>
   </td>
    <tr>
       <td align='center' valign='top' colspan='2'>
        <input type='submit' name='submit1'  value='Confirm'>
       </td>
	   </tr>
		</form>";
	
?>
Regards
Shehan31

Re: arrays and random number generators.

Posted: Sat Feb 05, 2011 2:17 am
by mellowman
This has been done before and there is a great tutorial about it :D

http://www.youtube.com/watch?v=EUBr-aHJzvw

Re: arrays and random number generators.

Posted: Sat Feb 05, 2011 3:46 am
by shehan31
mellowman wrote:This has been done before and there is a great tutorial about it :D
hi mellowman;
thank you for the reply. It helps me to some level but still i am looking for a method to store the contents of a variable in to an different arry. Therfore I can sort the order.
http://www.youtube.com/watch?v=EUBr-aHJzvw