math.randomseed(os.clock())
randomText = function (length, range)
local randText = ""
--math.randomseed(os.time()); Apparently this isn't necessary...
for k = 1, length do
local tval = math.floor(math.random(32, range))
randText = randText .. string.char(tval)
end
return randText
end
Muphry's law, he only does characters 1-31. :P
You should only ever seed in a program once.
Try running that function twice in immediate succession, you should get around the same output. This is because you are seeding to almost the same position each time, so you are going to end up getting the same output on calls to random.
the reason people avoid random seeding with time is because random(aSeed) is always equal to random(aSeed) where aSeed is the same. Sort of defeats the point of things being random! In C++ language the only (or the easiest) way to generate randoms is with a time seed, isn't it?
It's not that you avoid seeding with time, it's that you avoid seeding more than once at all. Think of rand() as returning a value in a giant array. That array is filled with random values, but each element in that array is always going to stay constant.
When you call rand(), it returns the value in that array at point i, then at next call it returns the value at point i+1, and so on. So when you seed with value 5, and then call rand a whole bunch, you can think of it as randArray[5], randArray[6], randArray[7]...
If you seed once to get a good starting point in that array, then you never have to worry about things not being random, as you chose a random start position, and the array progresses randomly. It (usually) contains every iteration of numbers possible too, so every sequence is possible to obtain.
However, if you seed twice, you always risk this happening: if I seed with 5, then make a few calls, I would get randArray[5], randArray[6], randArray[7]..., and then I seed with 6, and make those same calls, I would get randArray[6], randArray[7], randArray[8]...
Notice that only one of those numbers are different, randArray[6] and randArray[7] are equal to each other on each run. This is why it's a good idea never to seed more than once, so that you don't ever end up hitting the same index, and thus the same sequence of numbers.
Keep in mind that rand() doesn't actually use an array internally, but just an algorithm like an LFSR. It can still be thought of as an array though, and I find it useful to think of random generators this way when you don't actually know what algorithm they use.