Quantcast
Channel: Help! with 2.0 - Twine Forum
Viewing all articles
Browse latest Browse all 1844

Normal Distribution in Sugarcube 2

$
0
0
Hello everyone. First post here.

I tried searching for how to do normal distribution in Sugarcube 2 but nothing comes up, but I could find how to do it in javascript (which I know next to nothing about). I did find this javascript function that fits the bill for what I want and I want to know how I can implement it in the 'Edit Story Javascript' menu for use with Sugarcube 2:

// returns a gaussian random function with the given mean and stdev.
function gaussian(mean, stdev) {
var y2;
var use_last = false;
return function() {
var y1;
if(use_last) {
y1 = y2;
use_last = false;
}
else {
var x1, x2, w;
do {
x1 = 2.0 * Math.random() - 1.0;
x2 = 2.0 * Math.random() - 1.0;
w = x1 * x1 + x2 * x2;
} while( w >= 1.0);
w = Math.sqrt((-2.0 * Math.log(w))/w);
y1 = x1 * w;
y2 = x2 * w;
use_last = true;
}

var retval = mean + stdev * y1;
if(retval > 0)
return retval;
return -retval;
}
}


I found this at http://stackoverflow.com/questions/25582882/javascript-math-random-normal-distribution-gaussian-bell-curve.

At first I tried other methods that worked, but they didn't have mean and standard deviation variables which I need, but I have no idea how to convert this from javascript. So how can I get this to work in Sugarcube 2 so I can just write <<set $variable to gaussian(80, 10)>> for example?

Thanks in advance.

Viewing all articles
Browse latest Browse all 1844

Trending Articles