|
| 26 Jan 2015 02:45 PM |
| Generate a random letter from the alphabet using Scripting? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 26 Jan 2015 02:48 PM |
local arrayRandom = function (array) return (#array > 0) and array[math.random(1, #array)] or nil; end;
local stringToArray = function (str) local array = { }; for i = 1, #str, 1 do array[i] = str:sub(i, i); end return array; end;
local alphabet = "abcdefghijklmnopqrstuvxyz"; local randomLetter = arrayRandom(stringToArray(alphabet));
print(randomLetter); |
|
|
| Report Abuse |
|
|
|
| 26 Jan 2015 02:49 PM |
local alphabet = {"a", "b", "c", "d", "e"}
local random = alphabet[math.random(#alphabet)] |
|
|
| Report Abuse |
|
|
|
| 26 Jan 2015 02:51 PM |
@Chief
Does that really work? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 26 Jan 2015 02:51 PM |
... Just thought of any easier way:
local randomCharacter = function (upperCase) return string.char((uppercase and "A" or "a"):byte() + math.random(26) - 1); end; |
|
|
| Report Abuse |
|
|
|
| 26 Jan 2015 02:52 PM |
oh that's a quite creative solution drG
i like it |
|
|
| Report Abuse |
|
|
|
| 26 Jan 2015 02:55 PM |
Can you do something where it generates 3 letters, then 1 dash, then 3 numbers? Kind of like a license plate. |
|
|
| Report Abuse |
|
|
|
| 26 Jan 2015 02:57 PM |
| local str = randomCharacter(true) .. randomCharacter(true) .. randomCharacter(true) .. "-" .. math.random(1, 9) .. math.random(1, 9) .. math.random(1, 9) |
|
|
| Report Abuse |
|
|
|
| 26 Jan 2015 03:54 PM |
Since its already been answered, This isn't exactly what you asked for but I made this before and its kinda has to do with this
It makes a string of random lower and upper case letters, and numbers
local function random(x) local output = "" for _ = 1, (x or 16), 1 do output = (output .. ((math.random(3) == 1) and math.random(0, 9) or string[math.random(2) == 1 and "upper" or "lower"](string.char(math.random(97, 122))))) end return output; end
print(random()) -->random string 16 characters long print(random(4)) -->random string 4 characters long |
|
|
| Report Abuse |
|
|