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

Random String Generator

Started by darkrising, 15 May 2013 - 02:54 PM
darkrising #1
Posted 15 May 2013 - 04: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…
SpoilerIf you blur your eyes and look at this whilst its running you can sometimes see faces!



all = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
e = ""

while true do
  for i = 1, 51 do
	r = math.random(#all)
	e = e.. string.sub(all, r, r)
  end
  print(e)
  sleep(0.1)
end

Don't believe me? Take a look.
Spoiler
H4X0RZ #2
Posted 15 May 2013 - 05:27 PM
I like it, but the face thing is creepy :'(
darkrising #3
Posted 16 May 2013 - 03:41 PM
I like it, but the face thing is creepy :'(

They don't call me darkrising for nothing! :ph34r:/>
H4X0RZ #4
Posted 16 May 2013 - 03:53 PM
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:/>
FuuuAInfiniteLoop(F.A.I.L) #5
Posted 16 May 2013 - 07: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
Espen #6
Posted 17 May 2013 - 06: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].
FuuuAInfiniteLoop(F.A.I.L) #7
Posted 17 May 2013 - 12:51 PM
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","!","#","$","%","&amp;","/","(",")","="}
  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