[chuck-users] Coin in Chuck
Kassen
signal.automatique at gmail.com
Sun Aug 19 07:46:58 EDT 2012
On 19 August 2012 04:13, Mark Cerqueira <mark.cerqueira at gmail.com> wrote:
> You could do something like this:
>
> // generate a random float between 0.0 and 1.0 and compare its value to the
> percentage you would like doSomething() to be triggered
> if ( Std.rand2f(0.0, 1.0) < 0.20 )
> {
> // the code in here has a 20% chance of executing
> doSomething();
> }
>
> This is likely what's going on behind the scenes in the coin method in SC.
Or, as a function for re-use;
fun int coin(float x)
{
if (Std.rand2f(0,1)< x)
return 1;
else
return 0;
}
//usage;
<<<coin(.2)>>>;
There is also the "maybe" keyword which is not as versatile but
easier; it returns 1 or 0, with a 50/50 chance.
Yours,
Kas.
More information about the chuck-users
mailing list