Generating a random numbers and letters

前端 未结 15 1100
梦如初夏
梦如初夏 2020-12-25 08:23

Can I ask for a certain code on how to generate a random letters and numbers in one word. I know there\'s a PHP function rand(), but I don\'t know if it\'s also applicable w

相关标签:
15条回答
  • 2020-12-25 08:30

    Step 1: Create an alphabet, $alph = "0123456789abcde...";.

    Step 2: Create a random number, $n = rand(0, ALPHSIZE-1);, or use mt_rand().

    Step 3: Get the appropriate index in the alphabet: $alph[n];

    Rinse and repeat steps 2 and 3 for as many times as you need characters.

    If you want strong statistical properties (like uniformness), you should work a little harder with the random number, but this should get you started. (I think the statistical properties of that should be sufficient.)


    OK, might as well spell it out:

    $alph = "012...";
    function make_random_string($N)
    {
      $s = "";
      for ($i = 0; $i != $N; ++$i)
        s .= $alph[mt_rand(0, ALPHSIZE - 1)];
      return $s;
    }
    

    And here's the version that takes a custom alphabet:

    function make_random_custom_string($N, $alphabet)
    {
      $s = "";
      for ($i = 0; $i != $N; ++$i)
        s .= $alphabet[mt_rand(0, strlen($alphabet) - 1)];
      return $s;
    }
    

    Example: 10 random odd digits: make_random_custom_string(10, "13579");

    0 讨论(0)
  • 2020-12-25 08:33

    Try this, it worked for me.

    $numLenth = 35;
    
    function make_seed_Token() {
      list($usec, $sec) = explode(' ', microtime());
      return (float) $sec + ((float) $usec * 100000);
    }
    
    srand(make_seed_Token());
    
    $numSeed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $getNumber = "";
    for($i = 0; $i < $numLenth; $i ++) {
     $getNumber .= $numSeed[rand(0, strlen($numSeed))];
    
    }
    
    echo $getNumber;
    
    0 讨论(0)
  • 2020-12-25 08:33

    I put together a PHP class for generating random numbers and strings PHPRandomValue,

    It uses "mcrypt_create_iv(4, MCRYPT_DEV_URANDOM)" to generate random numbers and values. I made it while working on a crypto project because I needed a safe random value generator and mt_rand() doesn't meet that requirement. Here's an example usage

    $randomValue = new RandomValue;
    
    $randomValue->randomNumber(): = -3880998
    
    $randomValue->randomNumberBetween(1,10): = 2
    
    $randomValue->randomTextString(): = CfCkKDHRgUULdGWcSqP4
    
    $randomValue->randomTextString(10):  = LorPIxaeEY
    
    $randomValue->randomKey(): = C7al8tX9.gqYLf2ImVt/!$NOY79T5sNCT/6Q.$!.6Gf/Q5zpa3
    
    $randomValue->randomKey(10):  = RDV.dc6Ai/
    
    0 讨论(0)
  • 2020-12-25 08:35

    Use uniqid

    $desired_length = 10; //or whatever length you want
    $unique = uniqid();
    
    $your_random_word = substr($unique, 0, $desired_length);
    
    0 讨论(0)
  • 2020-12-25 08:35

    It might be done this way:

    function RandomCode($length = 10)
    {
        $code = '';
        $total = 0;
    
        do
        {
            if (rand(0, 1) == 0)
            {
                $code.= chr(rand(97, 122)); // ASCII code from **a(97)** to **z(122)**
            }
            else
            {
                $code.= rand(0, 9); // Numbers!!
            }
            $total++;
        } while ($total < $length);
    
        return $code;
    }
    
    0 讨论(0)
  • 2020-12-25 08:40
    function randText($len=4){
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        for($i=0;$i<$len;$i++){
            $txt.=substr($str, rand(0, strlen($str)), 1);   
        }
    return $txt;
    }
    

    to use simply type:

    $yourVar = randText(10);  // to get 10char long text
    
    0 讨论(0)
提交回复
热议问题