
Problem calling a function for a gaussian distribution, get [object Object] output !?
Hello everybody, I am new to JS and programming in general so please forgive my ignorance.
I have found online JS for creating a gaussian distribution and performing other functions on it. However, I have problems getting a reasonable output.
The code is found here: https://github.com/errcw/gaussian/bl...ib/gaussian.js
and function calls here: https://github.com/errcw/gaussian
I am calling the: gaussian(mean, variance) function in eclipse and I get an output [object Object].
Is there is someone to explain why I am having this problem?

Without delving too deep into this it doesn't appear as if anything is wrong. The guassian() function returns an object, not a single value. So essentially you need to put the return value in a variable like so:
Code:
var distribution = gaussian(mean, variance);
But remember that the variable 'distribution' is still an object, not a string or number so you can't simply display the variable on the screen or you will still see [object Object]. I'm not familiar with eclipse, but if this code is run on a webpage in any modern browser you can press F12 and then go to the 'Console' tab. Here you could add a line to your code to print this object to the developer console and this will allow you to see everything in the object.
Code:
var distribution = gaussian(mean, variance);
console.log(distribution);
I only suggest using the console to view the object since there is no 'var_dump()' (a PHP function) type of function in javascript that allows you to display an object/array's values on the page. But if none of this seems to be of any use to you I can download the guassian library and view the output for myself to give you a more direct way of getting your results.
"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Hello Sup3rkirby and thank you for your reply.
Well I do not know exactly how the JS engine code looks like but I cannot use an print function such as alert() or console.log(). It seems that the tool I use prints out the result of a function call automatically. So the output [object Object] is the actual printed output.
I had a similar problem in the past (in a different case) and I converted the output to a string value with ".toString()" function. I was expecting however a string output. I have also tried converting the output to a number with valueOf(), Number(), round() functions and I get an NaN (Not a Number) output. It is strange since I would expect a numerical value that is drawn from the gaussian distribution I want to create.
I'm stuck... Any help will much appreciated.

I'm not really sure what your full code looks like or what tool you seem to be using but because of how this function is designed to work the return value contains multiple bits of data and thus is an object (and must be treated as such).
I downloaded the gaussian code from GitHub and took a look to see what was in the object. There are 3 values stored: mean, standardDeviation and variance. Obviously the mean and variance were plugged into the function so I assume you want the value of standardDeviation.
Again I don't know how your tool works or what your actual code looks like but I guess you do have 2 ways of getting the value you want.
Code:
// Store the object and then call the value
var distribution = gaussian(mean, variance);
alert(distribution["standardDeviation"]);
// Directly call the value from the function
alert(gaussian(mean, variance)["standardDeviation"]);
I know you say you can't use alert() or console.log() but alert() was used in this example to simply show the values being printed out somewhere. Hopefully you do see how you could use the code (minus the alert() function) to adapt what you have and get your value directly.
"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Sup3rkirby the solutions you proposed indeed work!
It seems though that the gaussian function does not return any value but can be used as you show to pick values or in conjunction with other function calls given here: https://github.com/errcw/gaussian e.g., gaussian(mean, variance).pdf(x).
However, what I was looking for is creating a gaussian distribution and getting a random value from it based on the mean and variance values I set. I have realized that the code does not implement this. So there is a possibility here for an extension.
There is nothing more to do here I am afraid. Thank you very much for your effort I really appreciate it as I got to understand some things better

Code:
function playOne() {
var randSum=0;
for (var i=0; i < 20; i++)
randSum+= Math.random();
return (randSum * 0.7748)  7.748;
}
might work

To see object properties try :
Code:
console.log(JSON.stringify(distribution));

To simulate a Gaussian variable with mean 0 and variance 1, it suffices to calculate :
X = Math.sqrt(2*Math.log(Math.random())*Math.cos(2*Math.random());
Calculate Y = m + s X for the mean m and variance s !
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

