The spatial complexity is (1-N), and the time complexity should be the lowest. Lua's convenience is still worth using.
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
------------------- Do not repeat ------------------------------
Function random: getrandom (...)
???? Math. randomseed (tostring (OS. Time (): reverse (): Sub (); -- avoid time difference being too small
???? Math. Random (self. m_min, self. m_max); -- filter out the first few inferior random numbers;
???? 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 (); -- avoid time difference being too small
???? Math. Random (self. m_min, self. m_end); -- filter out the first few inferior random numbers;
???? 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;
Non-repeated random numbers implemented by Lua