Weird little Lua script - Random text

  • FeynmanLogomaker
    10th Jul 2013 Member 0 Permalink
    Script:


    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


    To use it, type in the console randomText(number of characters, ASCII character range). It generates a string of random characters.
  • mniip
    10th Jul 2013 Developer 0 Permalink
    1) "randText" is global
    2) line 3: a function shouldn't do that
    3) math.floor when given 2 arguments _always_ returns an integer value
    4) "tval" is global
    5) print? wtf... why not return

    4.5) not seeing how is range determining range exactly... none of characters in 0-31 are printable
  • MiningMarsh
    10th Jul 2013 Member 0 Permalink

    @mniip (View Post)

    Muphry's law, he only does characters 1-31. :P

  • FeynmanLogomaker
    10th Jul 2013 Member 0 Permalink
    @Mniip: Thanks, I'll do those...but what's wrong with randomseeding?
  • MiningMarsh
    10th Jul 2013 Member 0 Permalink

    @FeynmanLogomaker (View Post)

     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.

  • bimmo_devices
    11th Jul 2013 Member 0 Permalink

    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?

  • greymatter
    11th Jul 2013 Member 0 Permalink
    @bimmo_devices (View Post)
    To generate random numbers, I always use rand().
  • MiningMarsh
    11th Jul 2013 Member 0 Permalink

    @bimmo_devices (View Post)

     

    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.