Jump to content




Random String Generator


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

#1 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 15 May 2013 - 03:54 PM

Probably the shortest useful program I've ever written.

arg = {...}
if #arg == 0 then
  print("Usage: random <length>")
  return exit
end

all = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
e = ""

for i = 1, tonumber(arg[1]) do
  r = math.random(#all)
  e = e.. string.sub(all, r, r)
end

print(e)

What it does: Generates random strings when you type in a length! Amazing!

:mellow:

Something interesting...
Spoiler


#2 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 15 May 2013 - 04:27 PM

I like it, but the face thing is creepy :'(

#3 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 16 May 2013 - 02:41 PM

View PostFreack100, on 15 May 2013 - 04:27 PM, said:

I like it, but the face thing is creepy :'(

They don't call me darkrising for nothing! :ph34r:

#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 16 May 2013 - 02:53 PM

View Postdarkrising, on 16 May 2013 - 02:41 PM, said:

View PostFreack100, on 15 May 2013 - 04:27 PM, said:

I like it, but the face thing is creepy :'(

They don't call me darkrising for nothing! :ph34r:/>/>
Yes, they call you darkrising because it's your nickname :ph34r:

#5 FuuuAInfiniteLoop(F.A.I.L)

  • Banned
  • 435 posts
  • LocationThe left part of this post

Posted 16 May 2013 - 06:25 PM

Simpler one:
function get(lenght)
  str = ""
  for i=1, lenght do str = str..string.char(math.random(1, 255)) end
  return str
end
I havent tested it but it might work

#6 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 17 May 2013 - 05:11 AM

The problem is that the range of 1-255 (0-255 really, but it doesn't matter anyway because...) contains non-printable characters.
That's why darkrising defined a specific set of characters.

If you wanted to use the string.char() function on random numbers, you'd have to limit the set of possible random numbers to the amount of printable characters there are and then convert that random number up into the actual range of the printable characters.
But since the range of the actual printable characters is split, you'd actually end up with a lot more code to accommodate for that.
The range of printable characters being [32-95] and [97-126].

#7 FuuuAInfiniteLoop(F.A.I.L)

  • Banned
  • 435 posts
  • LocationThe left part of this post

Posted 17 May 2013 - 11:51 AM

function get(lengh, from)
  str=""
  all = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g", "h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J",  "K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","!","#","$","%","&","/","(",")","="}
  from = from or all
  for i=1, lenght do str = str..from[math.random(1, #all)] end
  return str
end
Corrected! and now you can send a table with the letters to chose from





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users