Jump to content




Random strings


  • You cannot reply to this topic
6 replies to this topic

#1 Selys

  • Members
  • 3 posts

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.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 27 December 2013 - 02:28 PM

Please post the code.

#3 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

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)


#4 Selys

  • Members
  • 3 posts

Posted 27 December 2013 - 02:49 PM

View PostLyqyd, on 27 December 2013 - 02:28 PM, said:

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.

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#6 Selys

  • Members
  • 3 posts

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)


#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users