Notice: Undefined offset: 828 in /home/maccabit/domains/maccabipedia.co.il/public_html/skins/Metrolook/customize/includes/app-header.php on line 59
יחידה:Random: הבדלים בין גרסאות בדף
מתוך מכביפדיה
(יצירת דף עם התוכן " local p = {} local function getBigRandom(l, u) -- Gets a random integer between l and u, and is not limited to RAND_MAX. local r = 0 local n = 2^math.random(30) -- Any power of 2. local limit = math.ceil(53 / (math.log(n) / math.log(2))) for i = 1, limit do r = r + math.random(0, n - 1) / (n^i) end return math.floor(r * (u - l + 1)) + l end function p.number(args) -- Gets a random number. first = tonumber(args[1]) second = tonumber(args[2])...")
 
אין תקציר עריכה
שורה 2: שורה 2:
local p = {}  
local p = {}  


local function getBigRandom(l, u)
 
-- Gets a random integer between l and u, and is not limited to RAND_MAX.
local r = 0
local n = 2^math.random(30) -- Any power of 2.
local limit = math.ceil(53 / (math.log(n) / math.log(2)))
for i = 1, limit do
r = r + math.random(0, n - 1) / (n^i)
end
return math.floor(r * (u - l + 1)) + l
end


function p.number(args)
function p.number(args)
שורה 17: שורה 8:
first = tonumber(args[1])
first = tonumber(args[1])
second = tonumber(args[2])
second = tonumber(args[2])
math.randomseed(os.time())
-- This needs to use if statements as math.random won't accept explicit nil values as arguments.
-- This needs to use if statements as math.random won't accept explicit nil values as arguments.
if first then
if first then
שורה 23: שורה 17:
first, second = second, first
first, second = second, first
end
end
return getBigRandom(first, second)
return math.random(first, second)
else
else
return getBigRandom(1, first)
return math.random(1, first)
end
end
else
else

גרסה מ־02:32, 9 במרץ 2025

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:Random/תיעוד


local p = {} 



function p.number(args)
	-- Gets a random number.
	first = tonumber(args[1])
	second = tonumber(args[2])
	
	math.randomseed(os.time())

	-- This needs to use if statements as math.random won't accept explicit nil values as arguments.
	if first then
		if second then
			if first > second then -- Second number cannot be less than the first, or it causes an error.
				first, second = second, first
			end
			return math.random(first, second)
		else
			return math.random(1, first)
		end
	else
		return math.random()
	end
end


return p