zoukankan      html  css  js  c++  java
  • 用lua实现的不重复随机数

    空间复杂度为(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;

  • 相关阅读:
    Java实现 LeetCode 211 添加与搜索单词
    跨平台Unicode与UTF8互转代码
    C++转换unicode utf-8 gb2312编码
    c++ ANSI、UNICODE、UTF8互转
    Visual C++ unicode and utf8 转换
    Unicode和UTF-8的关系
    boost uuid 学习笔记
    boost uuid
    Darwin Streaming server 的 Task 类
    VS2010下编译安装DarwinStreamingServer5.5.5
  • 原文地址:https://www.cnblogs.com/fegnze/p/4022786.html
Copyright © 2011-2022 走看看