[QUOTE=blackops7799;17088084][lua]
function util.ShuffleTable(t)
local n = #t
while n > 2 do
local k = math.random(n)
t[n], t[k] = t[k], t[n]
n = n - 1
end
return t
end
[/lua]
Shuffles a table. I used this in a lottery thing I made once.[/QUOTE]
[b][u][highlight]I KNOW THAT THIS CODE IS ENTIRELY EFFICIENT AND WORKS PERFECTLY AND ALL.[/highlight][/u]
I JUST WANT TO INFORM BLACKOPS ABOUT A CERTAIN FUNCTION HE COULD HAVE USED FOR THE PURPOSE.[/b]
[lua]
table.sort(the_table, function() return math.random() > 0.5 end)
[/lua]
[QUOTE=Deco Da Man;17099995][b][u][highlight]I KNOW THAT THIS CODE IS ENTIRELY EFFICIENT AND WORKS PERFECTLY AND ALL.[/highlight][/u]
I JUST WANT TO INFORM BLACKOPS ABOUT A CERTAIN FUNCTION HE COULD HAVE USED FOR THE PURPOSE.[/b]
[lua]
table.sort(the_table, function() return math.random() > 0.5 end)
[/lua][/QUOTE]
Easy there pal
[QUOTE=Deco Da Man;17099995][lua]
table.sort(the_table, function() return math.random() > 0.5 end)
[/lua][/QUOTE]
I was going to make a post like this, but there's a few things with your code there:
On larger tables, doing this ( which is similar to what I think you intended ):
[lua]
return math.random( 2 ) == 1
[/lua]
Will sometimes throw an error of "invalid sort function" or something of the like. The larger the table, the more likely this error is thrown ( eg, 100 entries almost always erred while 5 didn't very often ).
So it's not a consistent, reliable way of sorting the table from my experience.
[QUOTE=Kogitsune;17100378]I was going to make a post like this, but there's a few things with your code there:
math.random( [max/min:default 1], [max:default 1] )
So math.random( ) will always return 1 ( making the function always return true ).
[/QUOTE]
[code]Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(math.random())
0.0012512588885159
> print(math.random())
0.56358531449324
> print(math.random())
0.19330423902097
> print(math.random())
0.80874050111393
> print(math.random())
0.58500930814539
> print(math.random())
0.47987304300058
> print(math.random())
0.3502914517655
> print(math.random())
0.89596240119633
>[/code]
[lua]
for i = 1, 50 do
print( math.random( ) )
end
[/lua]
[code]
Running lua script '*untitled.lua' : Thu Sep 03 07:22:10 2009
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
End lua script : Thu Sep 03 07:22:10 2009
Execution time : 0.012(s)
[/code]
:S
Guess my IDE is just fucked up then.
Yep, looks like your IDE is fucked.
[editline]Edit: [/editline]
[img]http://i25.tinypic.com/2arvao.png[/img]
[QUOTE=Deco Da Man;17099995][b][u][highlight]I KNOW THAT THIS CODE IS ENTIRELY EFFICIENT AND WORKS PERFECTLY AND ALL.[/highlight][/u]
I JUST WANT TO INFORM BLACKOPS ABOUT A CERTAIN FUNCTION HE COULD HAVE USED FOR THE PURPOSE.[/b]
[lua]
table.sort(the_table, function() return math.random() > 0.5 end)
[/lua][/QUOTE]
Chill out. I didn't even make it. :v:
[url]http://en.wikipedia.org/wiki/Knuth_shuffle[/url]
Took it from there a awhile back.
I'm sure some one can make some thing better of this.
[lua]function typeWriterEffect( str, intvl, rnd)
if not str then return false end -- not obvious?
local rnd = rnd or 0 -- If no random interval is specified, give non.
local intvl = intvl or 0.5 -- If no intvl is specified for each key stroke, give defualt 0.3
local stlength = string.len( tostring(str) ) -- Number of chars in str, not obvious enough?
local newintvl = 0 -- Used for getting next key stroke time
local tbl = {}
tbl.newstr = "" -- Starter point
for i=1, stlength do
timer.Simple( math.Rand(newintvl-rnd, newintvl+rnd),
function()
tbl.newstr = tbl.newstr .. string.sub(str, i, i)
--table.insert( tbl, tbl.newstr )
print(tbl.newstr)
--surface.PlaySound( "ambient/machines/keyboard5_clicks.wav" )
end
)
newintvl = newintvl + intvl
end
--return tbl
end
[/lua]
[lua]
function printf( s, ... )
if not s or type( s ) ~= "string" then
print( ... )
else
print( s:format( ... ) )
end
end
[/lua]
Found this useful earlier today for debug purposes ( got tired of typing print( string.format( ... ) ) ). Forgive me if this already exists.
[lua]function utilx.Base10To(n,base)
if base < 2 or base > 64 then return false end
local bases = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local ret = ""
while n > 0 do
local div,rem = math.floor(n/base),math.fmod(n,base)
if rem >= 10 then rem = string.sub(bases,rem-9,rem-9) end
n = div
ret = ret..rem
end
return string.reverse(ret)
end[/lua]
Turns any base 10 number to a base between 2-64. I'll get one that converts it back in a bit.
[QUOTE=Entoros;17159912][lua]function utilx.Base10To(n,base)
if base < 2 or base > 64 then return false end
local bases = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local ret = ""
while n > 0 do
local div,rem = math.floor(n/base),math.fmod(n,base)
if rem >= 10 then rem = string.sub(bases,rem-9,rem-9) end
n = div
ret = ret..rem
end
return string.reverse(ret)
end[/lua]
Turns any base 10 number to a base between 2-64. I'll get one that converts it back in a bit.[/QUOTE]
Works the same way, I'm just bored.
[lua]
local base = { }
do
local bases = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
for i = 1, #bases do
base[ i + 9 ] = bases:sub( i, i )
end
end
function utilx.Base10To( x, nbase )
local nbase = math.Clamp( nbase || 2, 2, 64 )
local result = ""
repeat
local u = x % nbase
x = math.floor( x / nbase )
result = result .. ( base[ u ] || u )
until( x <= 0 )
return ( result:reverse( ) )
end
[/lua]
So, [url=http://www.lua.org/manual/5.1/manual.html#pdf-tonumber]tonumber[/url]?
[QUOTE=Entoros;17159912][lua]function utilx.Base10To(n,base)
if base < 2 or base > 64 then return false end
local bases = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local ret = ""
while n > 0 do
local div,rem = math.floor(n/base),math.fmod(n,base)
if rem >= 10 then rem = string.sub(bases,rem-9,rem-9) end
n = div
ret = ret..rem
end
return string.reverse(ret)
end[/lua]
Turns any base 10 number to a base between 2-64. I'll get one that converts it back in a bit.[/QUOTE]
[QUOTE=Nevec;17161084]So, [url=http://www.lua.org/manual/5.1/manual.html#pdf-tonumber]tonumber[/url]?[/QUOTE]
Entoros' can go up to base 64, whereas tonumber can only go up to base 36.
some functions I use in some places, mostly in wiremod.
[lua]-- works like pairs() except that it iterates sorted by keys.
-- criterion is optional and should be a function(a,b) returning whether a is less than b. (same as table.sort's criterions)
function pairs_sortkeys(tbl, criterion)
tmp = {}
for k,v in pairs(tbl) do table.insert(tmp,k) end
table.sort(tmp, criterion)
local iter, state, index, k = ipairs(tmp)
return function()
index,k = iter(state, index)
if index == nil then return nil end
return k,tbl[k]
end
end
-- sorts by values
function pairs_sortvalues(tbl, criterion)
local crit = criterion and
function(a,b)
return criterion(tbl[a],tbl[b])
end
or
function(a,b)
return tbl[a] < tbl[b]
end
tmp = {}
for k,v in pairs(tbl) do table.insert(tmp,k) end
table.sort(tmp, crit)
local iter, state, index, k = ipairs(tmp)
return function()
index,k = iter(state, index)
if index == nil then return nil end
return k,tbl[k]
end
end
-- like ipairs, except it maps the value with mapfunction before returning.
function ipairs_map(tbl, mapfunction)
local iter, state, k = ipairs(tbl)
return function(state, k)
local v
k,v = iter(state, k)
if k == nil then return nil end
return k,mapfunction(v)
end, state, k
end
-- like pairs, except it maps the value with mapfunction before returning.
function pairs_map(tbl, mapfunction)
local iter, state, k = pairs(tbl)
return function(state, k)
local v
k,v = iter(state, k)
if k == nil then return nil end
return k,mapfunction(v)
end, state, k
end[/lua]
(i)pairs_map is very useful in conjunction with unpack:
[lua]array = {
{1,"foo"},
{23,"bar"},
{42,"baz"},
}
for _,id,name in ipairs_map(array, unpack)
...
end
[/lua]
pairs_sortkeys/values iterate the array sorted by keys/values. You can even specify a criterion.
[QUOTE=TomyLobo;17206534]some functions I use in some places, mostly in wiremod.
pairs_sortkeys/values iterate the array sorted by keys/values. You can even specify a criterion.[/QUOTE]
That ipairs_map function looks pretty cool, nice job.
[lua]
function iff(cond, a, b) if cond then return a else return b end end
[/lua]
Equivalent to "cond ? a : b"
Not really that useful, but it has it's moments.
Lua has those..
[lua]local variable = ( cond ) and value1 or value2;[/lua]
[lua]function iff(cond, a, b)
return cond and a or b;
end[/lua]
[editline]01:49PM[/editline]
ninja'd!
Oh yeah... Silly me :P
Having it in a function makes it look fancy :3
And the real way can be confusing if it's used in between a lot of other code.
[QUOTE=Overv;17227323]And the real way can be confusing if it's used in between a lot of other code.[/QUOTE]
Not if you know what it means. And it's not your fault that the reader doesn't know.
keypress library for all keys:
[url]http://www.facepunch.com/showthread.php?t=775314[/url]
Usage:
[lua]function keypress.Add(key number,stringid string,func function,[funcunpress] function , [sendtoserver] boolean) [/lua]
(more info in the link)
"cond and a or b"
if a is nil or false, the whole thing breaks
[QUOTE=TomyLobo;17231005]"cond and a or b"
if a is nil or false, the whole thing breaks[/QUOTE]
If you know that there is a possibility that a and/or b is nil or false then you shouldn't be using this method.
I'm just saying that there still is a reason to have ?:/iff
rather ?: than iff, though
Purely client-side way to check whether the player can run console commands
[lua]if not CanRunConsoleCommand then
function CanRunConsoleCommand() return false end
hook.Add("OnEntityCreated", "CanRunConsoleCommand", function(ent)
if not ValidEntity(ent) then return end
if ent ~= LocalPlayer() then return end
function CanRunConsoleCommand() return true end
hook.Remove("OnEntityCreated", "CanRunConsoleCommand")
end)
end[/lua]
Usage: if CanRunConsoleCommand() then RunConsoleCommand("kill") end
EDIT: who rated this "dumb"? reply instead of just giving random ratings...
[lua]
function utilx.raw_factorial(n)
if n == 0 then
return 1
else
return n * utilx.raw_factorial(n - 1)
end
end
utilx.factorial = setmetatable({}, {
__newindex = function() end,
__index = function(self, k)
local factorial = utilx.raw_factorial(k)
rawset(self, k, factorial)
return factorial
end,
__call = function(self, ...)
local t = {...}
for i, n in pairs(t) do
t[i] = self[n]
end
return unpack(t)
end,
})
[/lua]
If everyone uses that or similar systems for mathematical functions where f(x) = y [b][i]always[/b][/i] then everyone's GMods would go a whole lot faster.
May not be a good idea for functions such as math.sin, due to the sheer number of values that will be calculated.
[QUOTE=Deco Da Man;17260831][lua]
function utilx.factorial(n)
if n == 0 then
return 1
else
return n * factorial(n - 1)
end
end
utilx.factorial = setmetatable({}, {
__newindex = function() end,
__index = function(self, k)
local factorial = utilx.raw_factorial(k)
rawset(self, k, factorial)
return factorial
end,
__call = function(self, ...)
local t = {...}
for i, n in pairs(t) do
t[i] = self[n]
end
return unpack(t)
end,
})
[/lua]
If everyone uses that or similar systems for mathematical functions where f(x) = y [b][i]always[/b][/i] then everyone's GMods would go a whole lot faster.
May not be a good idea for functions such as math.sin, due to the sheer number of values that will be calculated.[/QUOTE]
That's very useful, but where is utilx.raw_factorial defined?
[QUOTE=TehBigCheese;17260930]That's very useful, but where is utilx.raw_factorial defined?[/QUOTE]
hurr, thanks.
Whipped it up quickly, didn't bother testing :P
[QUOTE=Deco Da Man;17260950]hurr, thanks.
Whipped it up quickly, didn't bother testing :P[/QUOTE]
That's much more useful now :v:
Sorry, you need to Log In to post a reply to this thread.