Page 1 of 1

Random character?

Posted: Sat Nov 05, 2016 1:52 pm
by icekiller8002
heya guys

what I'm tryna do is I want to make it so the title gets set to a bunch of random characters. I used to play this game called ROBLOX (it uses lua aswell) and I was wondering how I'd do this in love.

This is how I'd do it in ROBLOX:
chars = {"a","b","0","%"}
rand = chars[math.random(1,#chars)]
love.window.setTitle(rand..rand..rand..rand..rand)

It could be a%b0b or any possible random character

How would I do this in love? Thank you if you can help!

Re: Random character?

Posted: Sat Nov 05, 2016 1:53 pm
by icekiller8002
Also, pardon me. I forgot to mention that the title always spews out as "%%%%%" (no quotes) when I try this.

Re: Random character?

Posted: Sat Nov 05, 2016 2:01 pm
by Nixola
That's because you're assigning a random character to a variable once, and then using that same variable (and so, the same character) five times. You need to have different variables for different characters. Also, you should use [wiki]love.math.random[/wiki] instead of math.random; it's got better "properties" and it's automatically seeded before [wiki]love.load[/wiki], so you will get different results each time you run your game.

Re: Random character?

Posted: Sat Nov 05, 2016 2:11 pm
by Manyrio
you could do something like that :

Code: Select all

chars = {"a","b","0","%"}
local function rand() return chars[love.math.random(1,#chars)] end
love.window.setTitle(rand()..rand()..rand()..rand()..rand())

Re: Random character?

Posted: Sat Nov 05, 2016 2:15 pm
by pgimeno
Yeah, in ROBLOX you'd actually get the same result with the code you posted: five repetitions of the same character.

Re: Random character?

Posted: Sun Nov 06, 2016 1:23 pm
by icekiller8002
for some reason, it's always 000b0 every time. is there a way to fix this?

Re: Random character?

Posted: Sun Nov 06, 2016 1:26 pm
by pedrosgali
Are you using love.math.random or just math.random? Math.random will always return the same numbers unless you set the seed at the start.

Re: Random character?

Posted: Sun Nov 06, 2016 3:17 pm
by pgimeno
Even if you're using love.math.random, you need to place that code in an event, e.g. love.load, or it won't get initialized in time.

Re: Random character?

Posted: Sun Nov 06, 2016 3:25 pm
by pedrosgali
I thought love.math.random was auto seeded before love.load... I could be wrong though.

Edit, never mind. I think I see what you meant now.

Re: Random character?

Posted: Fri Nov 11, 2016 11:03 am
by VectorNormal
I neat way to do this would be to use string.sub to select a single character from a string like so:

Code: Select all

math.randomseed(os.time())
list = "ab0%"
string = ""
for i = 1, 5 do
    local n = math.random(4)
    string = string .. string.sub(list, n, n)
end
love.window.setTitle(string)
A slightly different approach would be to use string.char to select characters at random. This would allow you to use all alphanumeric and special characters without having to build a list string first. You could do something like this:

Code: Select all

math.randomseed(os.time())
string = ""
for i = 1, 5 do
    string = string .. string.char(math.random(33, 126))
end
love.window.setTitle(string)