According to the lua-users wiki, a possible fix would be
Code: Select all
math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )
Code: Select all
math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )
When I was fiddling with his code, I even tried that seed, combined with a for-loop to call math.random() 50000 times and still it would give the exact number every time on 0.8.0 but also on 0.7.2.vrld wrote:Why is the first random number after randomseed() not random?
According to the lua-users wiki, a possible fix would beIt also affects only the first generated random value (and only BSD/OSX), so one additional math.random() after math.randomseed(os.time()) should be enough.Code: Select all
math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )
Remember that a sequence of numbers where no two neighboring numbers are the same is exactly the opposite of random. Repetitions will occur more frequently the fewer numbers you choose from. You can try it yourself by throwing a die multiple times.Lafolie wrote:I find that at least 3 randoms are needed before it starts spitting out anything different.
Code: Select all
function random_bag(values)
local bag = {}
local function refill()
for i,v in ipairs(values) do bag[i] = v end
-- shuffle the bag
for i=#bag,2,-1 do
local k = math.random(1,i)
bag[i], bag[k] = bag[k], bag[i]
end
end
return function()
if #bag == 0 then refill() end
return table.remove(bag)
end
end
Code: Select all
bag = random_bag({"music1.mp3", "music2.mp3", "music3.mp3"})
print(bag()) --> music1.mp3
print(bag()) --> music3.mp3
print(bag()) --> music2.mp3
print(bag()) --> music1.mp3
Users browsing this forum: Amazon [Bot], Bing [Bot] and 10 guests