This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Selys's profile picture

Random strings

Started by Selys, 27 December 2013 - 01:14 PM
Selys #1
Posted 27 December 2013 - 02:14 PM
– PS, I Wi-Fi jump


I made a random string maker, but there's a flaw.

It can only put letters in order, how can I scramble them?


posted from my tablet, I don't have the code. I'm all not at home, so I can't get it.
Lyqyd #2
Posted 27 December 2013 - 02:28 PM
Please post the code.
TheOddByte #3
Posted 27 December 2013 - 02:33 PM
Well how are you creating the random strings?
Let's say that you have all the letters in a table then you could do this

local letters = {
"A", "B", "C",
}
local str = ""
for i = 1,5 do
    str = str .. letters[math.random( 1, #letters )]
end
print(str)
Selys #4
Posted 27 December 2013 - 02:49 PM
Please post the code.

I did say I was no at one, and on a mobile device.


I have a variable with numbers and letters in it, then generate 2 numbers and use string.sub with the 2 numbers on the variable.
Lyqyd #5
Posted 27 December 2013 - 03:28 PM
I saw that. You still need to post the code. You probably should have waited until you could post the code before asking. You can't apply the advice given until you have the code anyways.

You should probably take a single character from your set of characters for as many times as you want. Just getting a substring of the character set is why you're always getting them in that order.
Selys #6
Posted 27 December 2013 - 05:55 PM
OK. Here's the code.


all = "1234567890ABCDEFGHIJKLMNZNOPZTUVQRSWXYZabcdefghjklnmoptuvqrswxys"
e = ""

local r1 = tonumber(math.random(1, 62))
local r2 = tonumber(math.random(r1, 62))

e = string.sub(r1, r2)
print(e)
Lyqyd #7
Posted 27 December 2013 - 08:17 PM
I fixed your code tags. Use square brackets for code tags instead of angle brackets.

You should use math.random (which returns a number, no need for tonumber()) to determine the length of string you want, then use a loop that will pick a single character from the string and add it to the output string. Iterate the loop as many times as the number of characters long the string should be. You also forgot to specify all as the first argument of the string.sub up there.