Page 1 of 1
Random string?
Posted: Sat Apr 07, 2012 3:45 pm
by aerhx
Hi, i'm fairly new to the love framework/lua.
I'm trying to do something like this (PHP):
How do I go about:
1. Making a random string
2. And attaching that random string to "_this"
Re: Random string?
Posted: Sat Apr 07, 2012 4:28 pm
by tentus
Code: Select all
local max = 32767 -- php's randmax on windows
x = math.random(0, max) .. "_this"
Note that at some earlier point (at least in pre-0.8.0 versions of love) you need to use math.randomseed() for the result to be properly random-seeming.
Re: Random string?
Posted: Sat Apr 07, 2012 7:17 pm
by Petunien
Just of curiosity, is it also possible to create a random string that contains letters?
How would that go?
Re: Random string?
Posted: Sat Apr 07, 2012 7:31 pm
by Nixola
You create a table called 'alphabet' (guess what you'll put in it) and do something like "var = math.random(26)", then search for the var index in alphabet
Re: Random string?
Posted: Sat Apr 07, 2012 7:37 pm
by Petunien
I started trying three minutes ago and use the same method you said...
A little challenge for me.
Re: Random string?
Posted: Sat Apr 07, 2012 7:54 pm
by Robin
Or:
Code: Select all
string.char(math.random(65, 90)) --uppercase
string.char(math.random(97, 122)) --lowercase
Re: Random string?
Posted: Sun Apr 08, 2012 1:14 am
by Jasoco
I created this function for generating UUID's for my game for no reason.
Code: Select all
function createUUID()
local uuid = ""
local chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for i = 1, 30 do
local l = math.random(1, #chars)
uuid = uuid .. string.sub(chars, l, l)
end
return uuid
end
You can place whatever characters you want in that string. It basically just chooses a character at random from it. But I guess you could also use string.char() which I didn't know about until just this moment. Either way would work fine I guess. Mine would only return letters and numbers unless modified.