Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

is there any way to generate a random string (text) or number?

Asked by
lytew 99
4 years ago

is there any way to generate a random string (text) or number?

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Hi Iytew!

To make a random string:

function doText(numLetters)
    local totTxt = ""
    for i = 1,numLetters do
        totTxt = totTxt..string.char(math.random(97,122))
    end
    print(totTxt)
end

doText(5) --Amount of letters in the string

*Legojoker created that script

Or if you want to choose a random string from a list of strings:

local avaliablestrings = {"Hello!", "Goodbye.", "How are you?"}
print(avaliablestrings[math.random(1, #avaliablestrings)]

To make a random number:

local chosenNumber = math.random(1, 20) --Generates a number between 1 and 20. Change it to whatever you want.

print(chosenNumber)

Hope I helped, LennyPlayzYT.

0
interesting! is there any way I can activate this script and not fall into the same word? lytew 99 — 4y
0
What do you mean? Which script are you talking about? LennyPlayzYT 269 — 4y
0
of the script you created. I am asking if there is any way to program the script so that this word (ToTxt) is not the same as the one generated previously lytew 99 — 4y
Ad
Log in to vote
0
Answered by
7z99 203 Moderation Voter
4 years ago
Edited 4 years ago

Yes, there is a way.

You would have to use math.random for that.

math.random(min, max) -- Generates an integer between your minimum and maximum value.
math.random(max) -- Generates an integer between 1 and your specified maximum value.
math.random() -- Generates a number between 0 and 1. It's going to be a decimal.

Here's how you would use it:

local string -- Blank variable, no value.
local number = math.random(3) -- Chooses a random number between 1 and 3

-- This is how you would generate a string with the math.random function.
if number == 1 then -- The two equal signs are what you use to compare two values. 
    string = "Option 1" -- The one equal sign is what you use to set a variable to a specific value.
elseif number == 2 then
    string = "Option 2"
elseif number == 3 then
    string = "Option 3"
end
0
That will be too complicated and just for the whole alphabet, you will need 25 elseifs and 1 if statement. LennyPlayzYT 269 — 4y
0
But it is possible to make it your way, but much simpler. Instead of doing string = "Option 1", you could do string = "Option "..number LennyPlayzYT 269 — 4y
0
Right, forgot about that haha/ 7z99 203 — 4y
0
is there any way not to fall in the same numbers? lytew 99 — 4y
0
What do you mean? 7z99 203 — 4y

Answer this question