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. Link to comment https://forums.phpfreaks.com/topic/215052-random-character-genorator/ 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. Link to comment https://forums.phpfreaks.com/topic/215052-random-character-genorator/#findComment-1118596 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; Link to comment https://forums.phpfreaks.com/topic/215052-random-character-genorator/#findComment-1118598 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. Link to comment https://forums.phpfreaks.com/topic/215052-random-character-genorator/#findComment-1118606 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.