Page 1 of 1

[SCRIPT] LUA os.clock() and os.time()

Posted: Fri Oct 12, 2012 5:48 am
by YpsiNine
Hi,

I guess these won't work within Grimrock as they don't belong to the math category?
I am doing some random number generation and would need a good seed for the math.randomseed function but the above doesn't seem to be implemented.

Thanks.

Re: LUA os.clock() and os.time()

Posted: Fri Oct 12, 2012 7:17 am
by YpsiNine
.
I came up with a solution of my own, yay!
The result is a random seed, but not as huge as os.clock() or os.time() would give I suppose, but it's better than nothing.

If you simply do random without a seed there will be very bad variation, like this:

Code: Select all

math.random(1,3)
Will typically produce:
3
3
3
1
3
3
3
3

Now with my code however, more randomness is to be expected. :)

Code: Select all

function randomize(x, y)
  seed = spawn("dagger", 1, 1, 1, 1)  --spawns a dagger at a place where the party can't interfere
  math.randomseed(seed.id)            --uses the ID of the spawned dagger as a random seed
  result = math.random(x, y)          --here we do the random number generation
  print(result)                       --to see the number it picked while debugging, else just remove this line
  print(seed.id)                      --to see the seed it got while debugging, else just remove this line
  seed:destroy()                      --then we destroy the dagger
  return result                       --we need to return the result to whatever asks for it
end
Will typically produce:
2
3
1
1
2
3
1
2

Use the function for example like this:

Code: Select all

variable = randomize(1,3)
if variable == 1 then
  hudPrint("I got a 1")
elseif variable == 2 then
  hudPrint("I got a 2")
elseif variable == 3 then)
  hudPrint("I got a 3")
end

Re: [SCRIPT] LUA os.clock() and os.time()

Posted: Fri Oct 12, 2012 7:26 pm
by YpsiNine
Bump if someone wants to include this script in the Useful scripts repository thread.

Re: [SCRIPT] LUA os.clock() and os.time()

Posted: Fri Oct 12, 2012 8:15 pm
by Komag
I think it's good to add it there because it's quite unique and will surely be useful in the future - you can just add a post to that thread yourself and put it in

Re: [SCRIPT] LUA os.clock() and os.time()

Posted: Fri Oct 12, 2012 8:43 pm
by YpsiNine
Komag wrote:I think it's good to add it there because it's quite unique and will surely be useful in the future - you can just add a post to that thread yourself and put it in
Ok, done. :)