Jump to content

Random Character Genorator


plznty

Recommended Posts

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

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.

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.