It looks like you're new here. If you want to get involved, click one of these buttons!

- 7.8K All Categories
- 11 Help with translations
- 4.2K General questions
- 106 Roadmap
- 358 Game & application design
- 315 Plugins
- 70 User experience
- 75 Marketplace
- 290 Code snippets
- 34 Building a team?
- 269 Suggestions & requests
- 352 Announce your apps made with Gideros.
- 90 Step by step tutorials
- 667 Bugs and issues
- 195 Introduce yourself
- 238 Announcements
- 106 Forum talk
- 403 Relax cafe

SinisterSoft
Maintainer

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...

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...

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

https://deluxepixel.com

+1 -1 (+2 / -0 )Share on Facebook

## Comments

Likes: SinisterSoft

Likes: totebo, antix

https://deluxepixel.com