Jump to content




How to EASLY make random strings of any length


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

#1 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 18 May 2013 - 05: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:
Posted Image
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


#2 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 18 May 2013 - 10: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....)

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 May 2013 - 10:49 AM

View PostMudkipTheEpic, on 18 May 2013 - 10:14 AM, said:

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...

#4 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 18 May 2013 - 10:51 AM

Fixed

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

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

Posted 18 May 2013 - 02: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

#6 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 18 May 2013 - 03:17 PM

Well, I haven't saw your code.

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

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

Posted 18 May 2013 - 05:24 PM

I posted mine in http://www.computerc...ing-randomizer/ and http://www.computerc...ring-generator/

#8 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 18 May 2013 - 07: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 by Pharap, 18 May 2013 - 10:26 PM.


#9 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 18 May 2013 - 09:37 PM

View PostPharap, on 18 May 2013 - 07:31 PM, said:

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.

#10 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 18 May 2013 - 10:26 PM

View PostOrwell, on 18 May 2013 - 09:37 PM, said:

View PostPharap, on 18 May 2013 - 07:31 PM, said:

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.

#11 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 19 May 2013 - 04:49 AM

View PostFuuuAInfiniteLoop&#40;F.A.I.L&#41;, on 18 May 2013 - 05:24 PM, said:

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users