plznty Posted October 3, 2010 Share Posted October 3, 2010 I would like to have a generator that basically creates a certain number of mixed characters. For example 6 characters which use A and B It would randomly produce things such as ABAABA BABABB I cannot think of any basic ways of achieving this. Any help would be appreciated even if its written rather than code. Thank you in advance. Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted October 3, 2010 Share Posted October 3, 2010 Try this out Ry4n0wnz... <?php $length = 6; $result = ''; $characters = array('A','B'); $countArray = (count($characters)-1); for($l=0; $l<$length; $l++) { $result .= $characters[mt_rand(0,$countArray)]; } echo $result; ?> Tell me how it goes bud Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Rifts Posted October 3, 2010 Share Posted October 3, 2010 function gen_chars($length = 6) { // Available characters $chars = 'AB'; $Code = ''; // Generate code for ($i = 0; $i < $length; ++$i) { $Code .= substr($chars, (((int) mt_rand(0,strlen($chars))) - 1),1); } return $Code; } // Usage $var = gen_chars(12); echo $var; Quote Link to comment Share on other sites More sharing options...
plznty Posted October 3, 2010 Author Share Posted October 3, 2010 Thanks both of you. I did make one in the mean time but my god it was badly done. This is much cleaner. Cheers both. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.