pickAndCall

Calls a function according to a probability

bool
pickAndCall
(
T
Fun
A...
)
(
T t
,
Fun fun
,
auto ref A a
)
if (
isNumeric!T &&
isCallable!Fun
&&
is(ReturnType!Fun == void)
)

Parameters

t T

The chance to call, in percentage.

fun Fun

The function to call. It must be a void function.

a A

The variadic argument passed to fun.

Return Value

Type: bool

false if no luck.

Examples

1 uint cnt;
2 bool test;
3 void foo(uint param0, out bool param1) @safe
4 {
5     cnt += param0;
6     param1 = true;
7 }
8 foreach(immutable i; 0 .. 100)
9     pickAndCall!(double)(75.0, &foo, 1, test);
10 assert(cnt > 25);
11 assert(test);
12 cnt = 0;
13 test = false;
14 foreach(immutable i; 0 .. 100)
15     pickAndCall!(byte)(0, &foo, 1, test);
16 assert(cnt == 0);
17 assert(!test);

Meta