<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/11/13
 * Time: 上午11:41
 */

namespace backend\helpers;


class RandomHelper
{
    /**
     * 生成一个随机的6位邮箱验证码
     * @param $length
     * @return string
     */
    public static function getRandomNo($length)
    {
        if ($length <= 0) {
            return "";
        }

        $randomChar = ['0','1','2','3','4','5','6','7','8','9'];
        $stringBuffer = '';
        for ($i = 0; $i < $length; $i++) {
            $stringBuffer .= $randomChar[abs(rand()) % count($randomChar)];
        }
        return $stringBuffer;
    }

    /**
     * 生成MT4密码
     */
    public static function getRandomStringForMt4()
    {
        return self::getRandomString(6).self::getRandomNo(1);
    }

    public static function getRandomString($length)
    {
        if ($length <= 0) {
            return "";
        }
        $randomChar = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
            'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
            'Y', 'Z'];
        $stringBuffer = '';
        for ($i = 0; $i < $length; $i++) {
            $stringBuffer .= $randomChar[abs(rand()) % count($randomChar)];
        }
        return $stringBuffer;
    }
}
