Providing default values when expected arguments not provided
Hello,
I am trying to built a random number generator with lower and upper
limits defined as arguments.
I try to provide default values in case no argument is typed in
however the following script doesn't work. It returns a NaN as output.
Any advice?
Roald Baudoux
// Random number generator
// inlets and outlets
inlets = 1;
outlets = 1;
//global variables
var random_minimum = jsarguments[1];
var random_maximum = jsarguments[2];
var random_range = random_maximum - random_minimum;
// random generation upon bang message
function bang()
{
if (random_minimum=="")
random_minimum = 0.;
else
random_minimum = random_minimum;
if (random_maximum=="")
vrandom_maximum = 1.;
else
random_maximum = random_maximum;
outlet (0, ((Math.random() * random_range)+random_minimum));
}
Salut Roald,
On 27 oct. 07, at 21:29, Roald Baudoux wrote:
> I am trying to built a random number generator with lower and upper
> limits defined as arguments.
>
> I try to provide default values in case no argument is typed in
> however the following script doesn't work. It returns a NaN as output.
You could initialize the min and max vars with your default values and
then check for the number of arguments.
Something like this:
// Random number generator
// inlets and outlets
inlets = 1;
outlets = 1;
//global variables
var random_minimum = 0.;
var random_maximum = 1.;
if (jsarguments.length==2)
{
random_minimum = jsarguments[1];
}
else if (jsarguments.length==3)
{
random_minimum = jsarguments[1];
random_maximum = jsarguments[2];
}
else if (jsarguments.length>3)
{
post("error: wrong number of arguments");
post();
}
var random_range = random_maximum - random_minimum;
// random generation upon bang message
function bang()
{
outlet (0, ((Math.random() * random_range)+random_minimum));
}