用lua实现的不重复随机数
2015-01-23 11:27
295 查看
空间复杂度为(1-n),时间复杂度为应该也是最低的,lua方便之处还是很值得利用的
local Random = {};
function Random:Awake()
self.m_min = 1;
self.m_max = 1;
self.m_end = 1;
self.m_rangeMap = {};
end
function Random:setRange( min,max )
if min > max then
min,max = max ,min;
end
self.m_min = min;
self.m_max = max;
self.m_end = max;
self.m_rangeMap[self.m_max] = self.m_max;
end
-------------------不重复------------------------------
function Random:getRandom( ... )
math.randomseed(tostring(os.time()):reverse():sub(1,6));--避免时差太小
math.random(self.m_min,self.m_max);--过滤掉前几个劣质随机数;
math.random(self.m_min,self.m_max);
math.random(self.m_min,self.m_max);
local tmp = math.random(self.m_min,self.m_max);
local ret = self.m_rangeMap[tmp];
if ret == nil then
ret = tmp;
end
self.m_rangeMap[tmp] = self.m_max;
self.m_max = self.m_max - 1;
return ret;
end
function Random:getRandomNormal( ... )
math.randomseed(tostring(os.time()):reverse():sub(1,6));--避免时差太小
math.random(self.m_min,self.m_end);--过滤掉前几个劣质随机数;
math.random(self.m_min,self.m_end);
math.random(self.m_min,self.m_end);
local ret = math.random(self.m_min,self.m_end);
local tmp = self.m_rangeMap[ret];
if tmp == nil then
self.m_rangeMap[ret] = self.m_max;
self.m_max = self.m_max - 1;
end
return ret;
end
return Random;
local Random = {};
function Random:Awake()
self.m_min = 1;
self.m_max = 1;
self.m_end = 1;
self.m_rangeMap = {};
end
function Random:setRange( min,max )
if min > max then
min,max = max ,min;
end
self.m_min = min;
self.m_max = max;
self.m_end = max;
self.m_rangeMap[self.m_max] = self.m_max;
end
-------------------不重复------------------------------
function Random:getRandom( ... )
math.randomseed(tostring(os.time()):reverse():sub(1,6));--避免时差太小
math.random(self.m_min,self.m_max);--过滤掉前几个劣质随机数;
math.random(self.m_min,self.m_max);
math.random(self.m_min,self.m_max);
local tmp = math.random(self.m_min,self.m_max);
local ret = self.m_rangeMap[tmp];
if ret == nil then
ret = tmp;
end
self.m_rangeMap[tmp] = self.m_max;
self.m_max = self.m_max - 1;
return ret;
end
function Random:getRandomNormal( ... )
math.randomseed(tostring(os.time()):reverse():sub(1,6));--避免时差太小
math.random(self.m_min,self.m_end);--过滤掉前几个劣质随机数;
math.random(self.m_min,self.m_end);
math.random(self.m_min,self.m_end);
local ret = math.random(self.m_min,self.m_end);
local tmp = self.m_rangeMap[ret];
if tmp == nil then
self.m_rangeMap[ret] = self.m_max;
self.m_max = self.m_max - 1;
end
return ret;
end
return Random;
相关文章推荐
- C#实现的不重复随机数序列生成算法
- 实现随机数不重复
- JavaScript 产生不重复的随机数三种实现思路
- 递归实现随机数不重复问题
- 算法--生成m个指定范围的不重复随机数的三种方法分析(Java实现)
- lua 不调用外部函数自己实现获取随机数
- cocos2d-x 实现不重复的随机数
- 如何利用C语言实现确定范围及不重复的随机数
- Java实现生成n个不重复的随机数
- Java编程实现生成给定范围内不重复随机数的方法小结
- Java 生成任意位数永不重复的随机数策略实现
- 高效产生一组不重复的随机数(受编程珠矶启示)java实现
- cocos2dx的runAction: 重复执行,多个动作连接执行,多个动作同时执行的实现(lua的应用)
- Java 生成任意位数永不重复的随机数策略实现
- JAVA实现指定区间取N个不重复随机数
- Python编程实现生成特定范围内不重复多个随机数的2种方法
- C++实现生成1—10000之间的10000个不重复的随机数
- 高效产生一组不重复的随机数(受编程珠矶启示)java实现