Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

MWC Random Number Generator
Started by Michael W. Sep 01 2015 06:38 PM

22 replies to this topic
release lua
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Michael W.

[GLOBAL: userInfoPane.html]
Michael W.
  • Contributor

  • 276 posts
  • Alumni

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

[TOPIC: post.html]
#2

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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?



[TOPIC: post.html]
#3

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,373 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,681 posts
  • Enterprise

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



[TOPIC: post.html]
#5

dkranj

[GLOBAL: userInfoPane.html]
dkranj
  • Observer

  • 5 posts
  • Corona SDK

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.



[TOPIC: post.html]
#6

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,681 posts
  • Enterprise

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.



[TOPIC: post.html]
#7

dkranj

[GLOBAL: userInfoPane.html]
dkranj
  • Observer

  • 5 posts
  • Corona SDK

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

 

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



[TOPIC: post.html]
#8

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 846 posts
  • Corona SDK

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.



[TOPIC: post.html]
#9

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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.



[TOPIC: post.html]
#10

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,681 posts
  • Enterprise

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



[TOPIC: post.html]
#11

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 846 posts
  • Corona SDK

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



[TOPIC: post.html]
#12

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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



[TOPIC: post.html]
#13

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 846 posts
  • Corona SDK

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



[TOPIC: post.html]
#14

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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



[TOPIC: post.html]
#15

Michael W.

[GLOBAL: userInfoPane.html]
Michael W.
  • Contributor

  • 276 posts
  • Alumni

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?



[TOPIC: post.html]
#16

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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?


[TOPIC: post.html]
#17

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 846 posts
  • Corona SDK

@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


[TOPIC: post.html]
#18

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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.



[TOPIC: post.html]
#19

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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'



[TOPIC: post.html]
#20

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 846 posts
  • Corona SDK

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



[TOPIC: post.html]
#21

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

I have tried the hotfix but it didn't help.

 

Regards,

Damir



[TOPIC: post.html]
#22

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 846 posts
  • Corona SDK

Okay, could you then try the pcall() thing again and let me know the error message?



[TOPIC: post.html]
#23

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

The error I get with pcall is:

 

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'




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: release, lua