So apologies if this has been asked before,
I am looking to build a sentence with random strings that will be stored in a table.
For example:
text = {}
names = {"a", "b", "c"}
name = names[rnd(2)] <-- doesn't work :(
foo = {
str = "Hi my name is" .. name
}
Everything I have found is in the Non P8 version of Lua.
1
name = names[flr(rnd(#names))+1] |
In Lua you can have non integer tables indices, you need to round the random value (float) to an int.
rnd() doesn't return an integer. also table indices start at 1:
name = names[1+flr(rnd(2))] |
if you need this a lot you could use:
function rndt(t) return t[1+flr(rnd(#t))] end name=rndt(names) |
Can save a token by using ceil instead of flr
name = names[ceil(rnd(2))] |
@Davbo: of course! ceil was (finally) added 6+ months ago. still not wired in my brain I guess ¯\_(ツ)_/¯
I'm not sure that's a good idea:
rnd() can return 0.0, and ceil(0) == 0.
[Please log in to post a comment]