在C++中
srand((unsigned)time(NULL)
cbPosition=rand()%10
这样就可以在【0,9】随机取值了,但是翻译成lua的时候就需要注意了
在lua中
cbPosition = math.random(0,10)
这样取出来的随机值是【0,10】
还有在短时间内取出来的数值都是一样的,加一句
math.randomseed(tostring(os.time()):reverse():sub(1,7))--设置时间种子
就可以解决了。
附加几个函数:
function GuanDanLogic:ZeroMemory(des, count, value)
if value == nil then
value = 0;
end
for i=1,count do
des[i - 1] = value;
end
end
function GuanDanLogic:CopyMemory(des, start, source, count)
local j = 1;
local i = start;
for k,v in pairs(source) do
if j <= count then
des[i] = v;
i = i +1;
end
j = j + 1;
end
end
还有附加几个运算方法:
--
-- Date: 2017-05-11 15:31:00
--
local bit={data32={}}
for i=1,32 do
bit.data32[i]=2^(32-i)
end
function bit:d2b(arg) --转换为二进制
local tr={}
for i=1,32 do
if arg >= self.data32[i] then
tr[i]=1
arg=arg-self.data32[i]
else
tr[i]=0
end
end
return tr
end --bit:d2b
function bit:b2d(arg) --转化为十进制
local nr=0
for i=1,32 do
if arg[i] ==1 then
nr=nr+2^(32-i)
end
end
return nr
end --bit:b2d
function bit:_xor(a,b)
local op1=self:d2b(a)
local op2=self:d2b(b)
local r={}
for i=1,32 do
if op1[i]==op2[i] then
r[i]=0
else
r[i]=1
end
end
return self:b2d(r)
end --bit:xor 异或运算(^)
function bit:_and(a,b)
local op1=self:d2b(a)
local op2=self:d2b(b)
local r={}
for i=1,32 do
if op1[i]==1 and op2[i]==1 then
r[i]=1
else
r[i]=0
end
end
return self:b2d(r)
end --bit:_and 与运算(&)
function bit:_or(a,b)
local op1=self:d2b(a)
local op2=self:d2b(b)
local r={}
for i=1,32 do
if op1[i]==1 or op2[i]==1 then
r[i]=1
else
r[i]=0
end
end
return self:b2d(r)
end --bit:_or 或运算( | )
function bit:_not(a)
local op1=self:d2b(a)
local r={}
for i=1,32 do
if op1[i]==1 then
r[i]=0
else
r[i]=1
end
end
return self:b2d(r)
end --bit:_not 对应 c ~ 非运算
function bit:_rshift(a,n)
local op1=self:d2b(a)
local r=self:d2b(0)
if n < 32 and n > 0 then
for i=1,n do
for i=31,1,-1 do
op1[i+1]=op1[i]
end
op1[1]=0
end
r=op1
end
return self:b2d(r)
end --bit:_rshift > > 右移运算
function bit:_lshift(a,n)
local op1=self:d2b(a)
local r=self:d2b(0)
if n < 32 and n > 0 then
for i=1,n do
for i=1,31 do
op1[i]=op1[i+1]
end
op1[32]=0
end
r=op1
end
return self:b2d(r)
end --bit:_lshift < < 左移运算
function bit:print(ta)
local sr=""
for i=1,32 do
sr=sr..ta[i]
end
print(sr)
end
return bit;