MWC Random Number Generator

MWC Random Number Generator

by Xibalba Studios

View and activate on the Corona Store

This is a simple, lightweight random number generator, adapted to Lua from some code by the late George Marsaglia (who maintained the famed Diehard test suite, for assessing various random number generators properties). It’s a slight update from the same module in the old Code Exchange.

This is a very lightweight generator, so it’s perfectly fine to maintain lots of instances, say one or more per object in a game. Moreover, each instance can be seeded by the user.

The upshot of this is that multiple random number sequences can be produced, these sequences are reproducible when seeded with the same values, and for that matter can be reproduced on e.g. a per-object basis, since there won’t be contention over one master generator.

Random number requests can be made for either a 32-bit integer or float between 0 and 1.

Furthermore, a math.random()-alike variant is available.

Documentation

In our card games we are using math.random for generating random numbers (for deck shuffling).

We had complains that cards dealt are not random.

We are considering using this plugin.

Can we say that this plugin produces “more” random sequences than the math.random?

fwiw, $0.02 - almost any other prng is better than math.random!

check its docs for a cycle length - make sure it’s really really really long

or look for a Mersenne Twister implementation in native lua.  (it won’t be “fast”, but need not be for your use)

and don’t constantly reseed with each shuffle (#seeds typically FAR less than cycle length) = biggest naive mistake

caveat:  if real money involved = regulation/certification may apply

Are you seeding the random number generator by calling math.randomseed( os.time() ) at the beginning of your main.lua?

We just did a tutorial this week on random numbers. See: https://coronalabs.com/blog/2015/12/22/tutorial-understanding-random-numbers-in-corona/

Rob

Thank you for your replies.

I have read many articles about random number generation.

In our games we have a option to turn on true random number generation. We than get the random sequences from random.org (they generate random numbers using atmospheric noise).

Yes, we use the randomseed(os.time()), that is a standard among all languages I would say.

I read that lua RNG generator is not good, that is way we will try to use this plugin.

Math.random() is a wrapper around the C Library’s rand() function which isn’t that great, but if you’re using random.org(), that’s about as random as you’re going to get.

Rob, for random.org customers must be connected to the net.

We wish to improve the PRNG , hopefully this plugin will help.

Hi, dkranj.

Between a day of travel yesterday and Christmas today, I only just saw this.

The algorithm’s designer, George Marsaglia, was a pretty big deal in the random number generator community. His diehard suites (“batteries” of tests) were all about evaluating the properties of various generators. I took that as a good recommendation.

That said, it would be more of a happy bonus than the express intent of the plugin, which was to be able to maintain unlimited independent generators that could also be saved and restored.

Dirk Laurie has also ported Bob Jenkins’s ISAAC to Lua, which you might also try, though you’d need to backport if from Lua 5.3.

If we wanna replace the math.random from lua with the plugin, could this work?

mwc = require(“plugin.mwc”)
_G.math.random = mwc.MakeGenerator_Lib{ w = os.time() }

We made some test and it seems this is the easiest way to replace it in the existing modules.

I’m not familiar with the mwc library, but as long as it’s parameter compatible with math.random, there would be no harm in doing what you’re doing. As a safety, you might want to do:

mwc = require("plugin.mwc") \_G.math.originalRandom = \_G.math.random \_G.math.random = mwc.MakeGenerator\_Lib{ w = os.time() }

Rob

@ubj3d.android Yes, this ought to work. The only thing to watch out for, if you happen to be require()'ing other modules, is that they might contain lines like this:

local random = math.random

Make sure to swap about math.random() before loading any of these others, or you’ll get mixed generators.

Is this plugin available on desktop ? (Windows, OS X)

When building for OS X, getting the error:

module ‘plugin_mvc’ not found:resource (plugin_mvc.lu) does not exist in archive

Assuming that’s a direct copy-and-paste, you just used the wrong acronym.  :slight_smile: m W c, for multiply-with-carry, not mvc (which is something else, but unrelated).

Unfortunately, that is not it.

It was not copy/paste, I typed that myself on my Windows machine while looking on the Mac, it is a typo.

The message is:

module ‘plugin_mwc’ not found:resource (plugin_mwc.lu) does not exist in archive

What does your build.settings look like? Do you have an “osx=true” and “win32-sim” in the supported platforms?

Hi Michael,

I did not had the “supported platforms” key in my build.settings.

But the documentation writes:

If there is no supportedPlatforms table specified for a plugin then it is assumed to be available on all platforms.

Anyway, I added it but got the same above error.

Here is my build.setting

settings = {

orientation = 

{

default = “landscapeRight”,

supported = { “landscapeRight”,“landscapeLeft”, }

},

    plugins =

{

–[“plugin.google.play.services”] = { publisherId = “com.coronalabs” }, – disable for Mac,

–[“CoronaProvider.analytics.flurry”] = { publisherId = “com.coronalabs”, }, – disable for Mac,

–[“plugin.kiip”] = { publisherId = “com.gremlininteractive” }, – disable for Mac,

–[“plugin.facebook.v4”] = { publisherId = “com.coronalabs” } – disable for Mac,  

[“plugin.mwc”] = { publisherId = “com.xibalbastudios”, supportedPlatforms = { iphone=true, android=true, osx=true } }

}, 

So the plugin is available on win32 and osx but I have some problems?

@ubj3d.android

I have my suspicions. Just to be sure, could you try require ()'ing with this code and tell me what you get?

local ok, mwc = pcall(require, "plugin.mwc") if not ok then print(mwc) -- in case of error, will be a message end

If I’m right, and it complains about “global bit” or similar, this is just something stupid on my part.

The module tries to use the bit library, if it’s loaded. I had to do some fixes to port this to plugin form, but I might have botched them.

If this is urgent and my guess is right, this ought to be a hotfix:

local old\_bit = package.loaded.plugin\_bit package.loaded.plugin\_bit = nil local mwc = require("plugin.mwc") package.loaded.plugin\_bit = old\_bit

I am confused, it suddenly works.

I tried your first suggestion 

local ok, mwc = pcall(require, "plugin.mwc") 

and there was no error. Then I returned the “classic” require and there is no error!

The only thing I changed is that I returned the build.settings to version without supportedPlatforms.

Did you made any changes to the plugin?

Anyway, thank you for your help and this great plugin.

Seems the error is still there.

Here is what I get when building for OS X:

module ‘plugin_mwc’ not found:resource (plugin_mwc.lu) does not exist in archive

no field package.preload[‘plugin_mwc’]
no file ‘/Users/ubj3d/Desktop/Ultimate BlackJack Reloaded.app/Contents/Plugins/plugin_mwc.lu’
no file ‘/Users/ubj3d/Desktop/Ultimate BlackJack Reloaded.app/Contents/Plugins/plugin_mwc.lua’
no file ‘/Users/ubj3d/Desktop/Ultimate BlackJack Reloaded.app/Contents/Resources/Corona/plugin_mwc.lua’
no file ‘/Users/ubj3d/Desktop/Ultimate BlackJack Reloaded.app/Contents/Plugins/plugin_mwc.dylib’
no file ‘/Users/ubj3d/Desktop/Ultimate BlackJack Reloaded.app/Contents/Resources/Corona/…/…/Frameworks/CoronaCards.framework/Versions/A/Frameworks/plugin_mwc.dylib’
no file ‘./plugin_mwc.dylib’
no file ‘/Users/ubj3d/Desktop/Ultimate BlackJack Reloaded.app/Contents/Resources/Corona/plugin_mwc.dylib’

Okay, then it might indeed be the issue I mentioned above.

Could you give the hotfix a shot? If that fixes it, I know what’s wrong.

I think the version I finally submitted is on an office computer at the moment (got a bit out of sync amid some connection woes), so it might take me a day or two to have it up. It’s probably only a few lines’ difference, but I just want to be sure I don’t undo anything else. I’ll take this opportunity to clean up my cluttered plugins situation, too.  :slight_smile: