duanfoumi5620 2012-04-03 18:35
浏览 43

编写一个函数Ge​​neratePassword,它接受两个参数,一个整数和一个由字母(a-z)和数字(0-9)组成的字符串[关闭]

Write a function GeneratePassword which accepts two arguments, an integer and a character string consisting of letters (a-z) and digits (0-9).

When GeneratePassword(5,'abc0123') is called, it should return a random string of 5 characters taken from 'abc0123'.

For Example : GeneratePassword(7,'abczxc012394') could return any of the following outputs :

2c00acb
2c23z93
030b2a4

  • 写回答

1条回答 默认 最新

  • duanjiong1952 2012-04-03 18:42
    关注

    I think you're looking for the homework tag.

    In the spirit of helping others I'll post a commented solution. However, keep in mind the only way to get better is to try first, ask questions later. That is to say, make an attempt then ask others where you went wrong.

    Example/Demo:

    /**
     * Generate a password N characters long consisting of characters
     *
     * @param int $size
     * @param string $characters
     * @param callback $random (optional) source of random, a function with two parameters, from and to
     * @return string|NULL password
     */
    function generate_password($size, $characters, $random = 'rand') {
    
        // validate $size input
        $size = (int) $size;
    
        if ($size <= 0) {
            trigger_error(sprintf('Can not create a password of size %d. [%s]', $size, __FUNCTION__), E_USER_WARNING);
            return NULL;
        }
    
        if ($size > 255) {
            trigger_error(sprintf('Refused to create a password of size %d as this is larger than 255. [%s]', $size, __FUNCTION__), E_USER_WARNING);
            return NULL;
        }
    
        // normalize $characters input, remove duplicate characters
        $characters = count_chars($characters, 3);
    
        // validate number of characters
        $length = strlen($characters);
        if ($length < 1) {
            trigger_error(sprintf('Can not create a random password out of %d character(s). [%s]', $length, __FUNCTION__), E_USER_WARNING);
            return NULL;
        }
    
        // initialize the password result
        $password = str_repeat("\x00", $size);
    
        // get the number of characters minus one
        // your string of characters actually begins at 0 and ends on the
        // string-length - 1:
        //   $characters[0] = 'a'
        //   $characters[1] = 'b'
        //   $characters[2] = 'c'
        $length--;
    
        // get one random character per each place in the password
        while ($size--)
        {
            // generate a random number between 0 and $length (including)
            $randomValue = $random(0, $length);
            // that random number is used to turn the number into a character
            $character = $characters[$randomValue];
            // set the random character
            $password[$size] = $character;
        }
    
        // return the result
        return $password;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算