• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

math.random() on startUp - The seed is bugged, here how to fix

kito2

www.masteria.net
Joined
Mar 9, 2009
Messages
3,764
Solutions
1
Reaction score
227
Location
Chile, Santiago
Hey everyone,

Today was making a few test and noticed that math.random(0,any_number) will ALWAYS throw the same first random number since the seed which feeds the generator is constant.

A good way to solve this is:

Code:
math.randomseed(os.time())

Hope this help someone.
 
https://github.com/otland/forgottenserver/blob/master/data/globalevents/scripts/startup.lua#L2
use os.mtime() because it is std::chrono, which is safer over os.time() because it takes CPU clocks (https://github.com/otland/forgottenserver/blob/master/src/tools.h#L95-L98), which mean you will have a number really greater than default lua os.time().

The problem is if you need to create different random numbers... They will keep creating same numbers... Don't know why happened that.
 
The problem is if you need to create different random numbers... They will keep creating same numbers... Don't know why happened that.
probably its your distro, in currently tfs code are ok, i meant ofc math.randomseed(os.mtime()), if you put this onServerUp event in globalevents you are suppose to get random seeded.
 
Not really a bug. You just have to use it inside a function on the script, or randomize the seed if you really really need to generate the random element outside functions.
 
Today was making a few test and noticed that math.random(0,any_number) will ALWAYS throw the same first random number since the seed which feeds the generator is constant.

A good way to solve this is:

Code:
math.randomseed(os.time())

Hope this help someone.
Code:
But beware! The first random number you get is not really 'randomized' (at least in Windows 2K and OS X). To get better pseudo-random number just pop some random number before using them for real:
-- Initialize the pseudo random number generator
math.randomseed( os.time() )
math.random(); math.random(); math.random()
-- done. :-)

http://lua-users.org/wiki/MathLibraryTutorial

It's a good practice to do this always.
 
Not a bug nor a discussion just a tutorial
tbh its a very basic thing that everyone who studied a little knows
 
Not a bug nor a discussion just a tutorial
tbh its a very basic thing that everyone who studied a little knows

You would get impressed about how people could be sometimes. Specially in my case when you are rusty for having a few years out of a community like this.

This could help someone, that's the idea. So it may be like a tutorial.
 
You would get impressed about how people could be sometimes. Specially in my case when you are rusty for having a few years out of a community like this.

This could help someone, that's the idea. So it may be like a tutorial.
yea
but my point is just that this thread is in the wrong subforum
it would be useful for who is willing to learn scripts using OTLand content

"How to change randomseed"
 
Back
Top