Random character?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
icekiller8002
Prole
Posts: 49
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

Random character?

Post 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!

Code: Select all

function love.draw()
  love.graphics.print("obey")
end
User avatar
icekiller8002
Prole
Posts: 49
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

Re: Random character?

Post by icekiller8002 »

Also, pardon me. I forgot to mention that the title always spews out as "%%%%%" (no quotes) when I try this.

Code: Select all

function love.draw()
  love.graphics.print("obey")
end
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Random character?

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Manyrio
Prole
Posts: 29
Joined: Sat Feb 06, 2016 10:12 am

Re: Random character?

Post 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())
function love.load() end
function love.update(dt) end
function love.draw() end
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Random character?

Post by pgimeno »

Yeah, in ROBLOX you'd actually get the same result with the code you posted: five repetitions of the same character.
User avatar
icekiller8002
Prole
Posts: 49
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

Re: Random character?

Post by icekiller8002 »

for some reason, it's always 000b0 every time. is there a way to fix this?

Code: Select all

function love.draw()
  love.graphics.print("obey")
end
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Random character?

Post 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.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Random character?

Post 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.
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Random character?

Post 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.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
VectorNormal
Prole
Posts: 8
Joined: Sun Oct 02, 2016 5:58 am

Re: Random character?

Post 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)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 80 guests