When writing a game it's often useful to be able to get a random number. It's even more useful if you can fix the sequence of random numbers so they are the same from one computer OS to another. This way you can make games that levels are determined by a random number 'seed' and that the levels will be consistent on all the platforms your game runs on.
Unfortunately the normal math.random function can't do this, it ultimately relies on the random number function provided by the host operating system.
So.. In Gideros (v2017.9+) there are now a couple of alternative random number functions.
random=Core.random([generator][,bound1][,bound2])
seed=Core.random([generator][,seed])
The first function operates in almost the same way as the existing math.random function except that it has an additional first parameter, this will set the random number method - just leave this at 0 (MT19937
https://en.wikipedia.org/wiki/Mersenne_Twister) for now. Note that for random numbers produced between 0 and 1 then there may be very slight differences dependent on floating point number systems between different computer types - but integer sequences should be exactly the same.
The second function will either set a new seed for the generator and/or return the existing seed - so you can save it in a game save file / whatever.
@hgy29 did some quick tests and found that although very very slightly slower than the normal math.random function there was no noticeable disadvantage to using the new function. The new functions also allow for different generators, this means that faster - but less random - methods could be added in the future alongside methods that return multiple values, etc...
Comments
Likes: SinisterSoft
Likes: totebo, antix
https://deluxepixel.com