Random string?

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
aerhx
Prole
Posts: 1
Joined: Sat Apr 07, 2012 3:42 pm

Random string?

Post by aerhx »

Hi, i'm fairly new to the love framework/lua.

I'm trying to do something like this (PHP):

Code: Select all

<?php 
$x = rand().'_this';
?>
How do I go about:
1. Making a random string
2. And attaching that random string to "_this"
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Random string?

Post 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.
Kurosuke needs beta testers
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: Random string?

Post by Petunien »

Just of curiosity, is it also possible to create a random string that contains letters?

How would that go? :)
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Random string?

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: Random string?

Post by Petunien »

I started trying three minutes ago and use the same method you said... ^^
A little challenge for me.
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Random string?

Post by Robin »

Or:

Code: Select all

string.char(math.random(65, 90)) --uppercase
string.char(math.random(97, 122)) --lowercase
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Random string?

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

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 8 guests