Creating a 14 character 'random key generator'

安稳与你 提交于 2020-01-16 13:19:08

问题


I'm trying to use CodeIgniter to write up a small program for school which generates a random 'key' every time I click the 'generate' button. Looking to see if there's a way for me to create a function where I can fill up a 14 character array with a random number or letter and then set the array to a variable which I can call upon to display as my generated key.

Any and all help would be much appreciated as I am new to CodeIgniter.


回答1:


A while back I wrote this function in PHP, it does what it does and gives you some flexibility as well through complexity modifiers, I used a default set of 5 different 'levels' of characters and the length is also variable ofcourse.

I'm just going to chuck it in here and 'try' to explain what is going on as well as I can by comments:

function rsg($length = 10, $complexity = 2) {
        //available 'complexity' subsets of characters
        $charSubSets = array(
            'abcdefghijklmnopqrstuvwxyz',
            'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
            '0123456789',
            '!@#$%^&*()_+{}|:">?<[]\\\';,.`~',
            'µñ©æáßðøäåé®þüúíóö'
        );

        // will be filled with subsets from above $charSubsets
        $chars = '';

        //concact each subset until complexity is reached onto the $chars variable
        for ($i = 0; $i < $complexity; $i++)
            $chars .= $charSubSets[$i];

        //create array containing a single char per entry from the combined subset in the $chars variable.
        $chars = str_split($chars);
        //define length of array for mt_rand limit
        $charCount = (count($chars) - 1);
        //create string to return
        $string = '';
        //idk why I used a while but it won't really hurt you when the string is less than 100000 chars long ;)
        $i = 0;
        while ($i < $length) {
            $randomNumber = mt_rand(0, $charCount); //generate number within array index range
            $string .= $chars[$randomNumber]; //get that character out of the array
            $i++; //increment counter
        }

        return $string; //return string created from random characters
    }

This is what I currently use and it has satisfied my needs for quite some time now, if anyone reading over this has improvements I'd love to hear them as well!




回答2:


$a=array(rand(10000000000000, 99999999999999));

is a quick way to get a 14 digit array.




回答3:


It depends on how random you want it to be. You could specify all characters you want in a $characters string, then just create a string up to $length, picking a random substring of length 1 from the characters string.

What are the requirements? Do you want it to be as random as possible (This link might be useful) Are multiple occurrences of one character allowed in one random string?

Here's an example though: PHP random string generator



来源:https://stackoverflow.com/questions/28290311/creating-a-14-character-random-key-generator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!