用lua实现的不重复随机数
来源:互联网 发布:网络电影怎么挣钱 编辑:程序博客网 时间:2024/05/22 05:00
空间复杂度为(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;
- 用lua实现的不重复随机数
- lua取不重复随机数
- 实现随机数不重复
- cocos2d-x 实现不重复的随机数
- 不重复的随机数
- 不重复的随机数
- 用C#生成不重复的随机数
- 用C#生成不重复的随机数
- 用C#生成不重复的随机数
- 用C#生成不重复的随机数
- 用C#生成不重复的随机数
- 用C#生成不重复的随机数
- 用Random获取不重复的随机数
- 用C#生成不重复的随机数
- 用C#生成不重复的随机数
- 实现从区间产生不重复的随机数 - JS 实现
- 例子---随机数/不重复的随机数
- 产生不重复的随机数
- jdk环境变量配置和问题分析
- vim VI E138: Can't write viminfo file /*/.viminfo!
- 树
- 只有百度没有google可不行
- Stanford机器学习课程笔记——单变量线性回归和梯度下降法
- 用lua实现的不重复随机数
- c/c++函数和指针
- pflua, pflang, a luajit, in kernel engine for packet filter
- MySql安装心得
- c++基础(宏)
- lua基础篇(二)
- Android 三种方式实现自定义圆形页面加载中效果的进度条
- 生活常识
- C++基础(using)