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

How to EASLY make random strings of any length

Started by jesusthekiller, 18 May 2013 - 04:31 AM
jesusthekiller #1
Posted 18 May 2013 - 06:31 AM
Hi all!
I've recently saw new 2 posts in Programs section with an solution for generating random strings. One of them used table of chars (O.o), second one was choosing a random character from a string-type matrix (Smart idea).

I don't say that any of those methods were incorrect, but there is one even simpler:

First of all, there is a thing called ASCII (If you don't know what it is - google it up). There is full table:

We are interested only in characters from 32 (Space) to 126 (~) - characters which can be understood by humans.

Second bit of a puzzle is math.random( a, b ) function, which generates an random number in range from a to b.

And third and last is string.char( i, […] ) which turns number(s) into character(s).

So you probably now have an idea how to do it :)/>, but if you don't that's how I did it:


function makeString(l)
	if l < 1 then return nil end -- Check for l < 1
	local s = "" -- Start string
	for i = 1, l do
		s = s .. string.char(math.random(32, 126)) -- Generate random number from 32 to 126, turn it into character and add to string
	end
	return s -- Return string
end

And if you don't want gave key generated, do something like that (thanks mudkip):


function makeString(l)
	if l < 1 then return nil end -- Check for l < 1
	local s = "" -- Start string
	for i = 1, l do
	    n = math.random(32, 126) -- Generate random number from 32 to 126
	    if n == 96 then n = math.random(32, 95) end
		s = s .. string.char(n) -- turn it into character and add to string
	end
	return s -- Return string
end
MudkipTheEpic #2
Posted 18 May 2013 - 11:14 AM

local stringtable={}
for i=32,126 do if not i==96 then table.insert(stringtable,string.char(i)) else table.insert(stringtable,"?") end end
function makeString(l)
  if l < 1 then return nil end
  local stringy=""
  for i=1,l do
    stringy=stringy..stringtable[math.random(#stringtable)]
  end
  return stringy
end

This might be the more preferable code, since you can't print a "`" (the grave key)

(Although, maybe it already turns ` into a ?… Hmm….)
theoriginalbit #3
Posted 18 May 2013 - 11:49 AM
This might be the more preferable code, since you can't print a "`" (the grave key)

(Although, maybe it already turns ` into a ?… Hmm….)
` is not turned into ?, its just unprintable, hence it being displayed as a ?, but if you were to be generating a string for something like an encryption key it would be perfectly fine to have ` in it…
jesusthekiller #4
Posted 18 May 2013 - 11:51 AM
Fixed
FuuuAInfiniteLoop(F.A.I.L) #5
Posted 18 May 2013 - 03:53 PM
The first code looks similar to a code that i made in another topic(i put 1, 255) instead of that and my string was str intead of s
jesusthekiller #6
Posted 18 May 2013 - 04:17 PM
Well, I haven't saw your code.
FuuuAInfiniteLoop(F.A.I.L) #7
Posted 18 May 2013 - 06:24 PM
I posted mine in http://www.computercraft.info/forums2/index.php?/topic/12827-string-randomizer/ and http://www.computercraft.info/forums2/index.php?/topic/12837-random-string-generator/
Pharap #8
Posted 18 May 2013 - 08:31 PM
Not sure if it's true for LuaJ (the library CC uses as it's VM), but concatenating tables is actually faster and more memory efficient than concatenating strings, so I'd recommend testing your function using tables of chars instead of a single string:

function RandomString(length)
	   length = length or 1
		if length < 1 then return nil end
		local array = {}
		for i = 1, length do
			array[i] = string.char(math.random(32, 126))
		end
		return table.concat(array)
end
Edited on 18 May 2013 - 09:26 PM
Orwell #9
Posted 18 May 2013 - 10:37 PM
Not sure if it's true for LuaJ (the library CC uses as it's VM), but concatenating tables is actually faster and more memory efficient than concatenating strings, so I'd recommend testing your function using tables of chars instead of a single string:

function RandomString(length)
	   length = length or 1
		if length < 1 then return nil end
		local array = {}
		for i = 1, length do
			array[i] = string.char(math.random(32, 126))
		end
		return array.concat
end
You made a small mistake. You need to have:

return table.concat(array)
Even 'array:concat()' doesn't work as plain tables obviously don't have a metatable set.
Pharap #10
Posted 18 May 2013 - 11:26 PM
Not sure if it's true for LuaJ (the library CC uses as it's VM), but concatenating tables is actually faster and more memory efficient than concatenating strings, so I'd recommend testing your function using tables of chars instead of a single string:

function RandomString(length)
	   length = length or 1
		if length < 1 then return nil end
		local array = {}
		for i = 1, length do
			array[i] = string.char(math.random(32, 126))
		end
		return array.concat
end
You made a small mistake. You need to have:

return table.concat(array)
Even 'array:concat()' doesn't work as plain tables obviously don't have a metatable set.

Sorry, forgot, I've got too used to C# since I haven't been using lua much lately.
Well I have, just not for small things like that.
Engineer #11
Posted 19 May 2013 - 05:49 AM
Why do you make a problem for something ANY programmer can come up? Im literally facepalming here…

To get back on topic: this is better then how some people do it, nice!