LUA随机
参数1. rnType 表示随机类型
1为输入随机数字
2为随机手机号
3为随机字母
4为随机字母/数字(先字母后数字),一般用于输用户名和密码,所以字母在前
5为随机邮箱
6为随机16进制
7为随机中文(常用中文字库到度娘下载吧)
参数2. rnLen 表示随机的长度
参数3. rnUL 表示字母的大小写。1为大写、2为小写、其他为不区分,默认为不区分
以上三个参数,用不到的参数就不用填,用不到的参数你设置了不会出错,但也不会生效。
比如手机号只要一个rnType参数就行,生成数字就只要rnType、rnLen参数
如果随机结果有字母,且不区分大小写的话,也不用rnUL参数
脚本最后有示例,直接调试下就看出来效果了
---------------------------------------------------------------------
新增加随机函数,生成通过校验的苹果IMEI
可以直接添加到myRand里,比如设成8,需要用的时候直接myRand(8)调用
--------------------------------------------------------------------
本帖隐藏的内容
[mw_shl_code=php,true]--[[参数1. rnType 表示随机类型
1为输入随机数字
2为随机手机号
3为随机字母
4为随机字母/数字(先字母后数字),一般用于输用户名和密码,所以字母在前
5为随机邮箱
6为随机16进制
7为随机中文(常用中文字库到度娘下载吧)
参数2. rnLen 表示随机的长度
参数3. rnUL 表示字母的大小写。1为大写、2为小写、其他为不区分,默认为不区分
以上三个参数,用不到的参数就不用填,用不到的参数你设置了不会出错,但也不会生效。
比如手机号只要一个rnType参数就行,生成数字就只要rnType、rnLen参数
如果随机结果有字母,且不区分大小写的话,也不用rnUL参数
脚本最后有示例,直接调试下就看出来效果了
]]
function myRand(rnType,rnLen,rnUL)
local zmRan,HexRan,myrandS,rns
rnUL=rnUL or 3
rns=rns or 0 --用于精确随机种子
rns=rns+1
zmRan={"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"}
HexRan={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","A","B","C","D","E","F"}
myrandS=""
math.randomseed(rns..tostring(os.time()):reverse():sub(1, 6))
if rnType==1 then --生成数字
myrandS=math.random(9)
for r1=1,rnLen-1 do
myrandS=myrandS..math.random(0,9)
end
elseif rnType==2 then --生成手机号,rnLen,rn11无需设置
local mheader={"13","15"}
myrandS=mheader[math.random(#mheader)]
for r1=1,9 do
myrandS=myrandS..math.random(0,9)
end
elseif rnType==3 then --生成字母
for r1=1,rnLen do
myrandS=myrandS..zmRan[math.random(52)]
end
elseif rnType==4 or rnType==5 then --生成数字/字母组合或邮箱
local rn3=math.random(2,5)
for r1=1,rn3 do
myrandS=myrandS..zmRan[math.random(52)]
end
for r1=1,rnLen-rn3 do
myrandS=myrandS..math.random(0,9)
end
if rnType==5 then
local mailheader={"@qq.com","@hotmail.com","@sohu.com"} --自行增减
myrandS=myrandS..mailheader[math.random(#mailheader)]
end
elseif rnType==6 then --生成16进制
myrandS=HexRan[math.random(2,22)]
for r1=1,rnLen-1 do
myrandS=myrandS..HexRan[math.random(22)]
end
elseif rnType==7 then --生成中文
if ZW_txt==nil then
file_zw=io.open("/User/Media/TouchSprite/plugin/中文字库.txt","r")
ZW_txt=file_zw:read("*all")
end
ZW_txt=ZW_txt or ""
if ZW_txt=="" then return "无字库" end
for r1=1,rnLen do
local aaa=math.random(1,#ZW_txt/3)
myrandS=myrandS..string.sub(ZW_txt,aaa*3+1,aaa*3+3)
end
return myrandS
end
if rnUL==1 then
return string.upper(myrandS) --返回大写
elseif rnUL==2 then
return string.lower(myrandS) --返回小写
else
return myrandS
end
end
print(myRand(1,9))
print(myRand(2))
print(myRand(3,9,1))
print(myRand(4,9,2))
print(myRand(5,9,""))
print(myRand(6,9))
print(myRand(7,9))
[/mw_shl_code]