Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Unified random number generator — Gideros Forum

Unified random number generator

SinisterSoftSinisterSoft Maintainer
edited September 2017 in Code snippets
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...

Likes: totebo, antix

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+2 / -0 )Share on Facebook

Comments

Sign In or Register to comment.