# How to execute a function with a certain probability

• 09-10-2010
siddhabathula
How to execute a function with a certain probability
Hi All,

I would like to have a function which is executed with a certain probability say 0.1 or 0.2.

i.e. If I have a function say

caller()
{
called_probability(0.1);
}

whenever the function caller is called the function called_probability will execute with the probability of the argument, which in this case is 0.1.

How can I do so?

Kartik
• 09-10-2010
brewbuck
You could generate a random integer by calling rand(), then dividing by RAND_MAX to get a value between 0.0 and 1.0 -- then, call the function if the value is <= 0.1:

Code:

double rand_value()
{
return (double)rand() / RAND_MAX;
}

...

if (rand_value() <= 0.1)
func();

Of course, rand() is not a very good random number generator, so if you doing anything of mathematical, engineering, or scientific significance you should really consider a better RNG.
• 09-10-2010
new_ink2001
Siddhabathula,

At the risk of doing a homework problem for a fellow poster, I would suggest looking into the function rand().

According to the Wikipedia: "Most pseudorandom generator algorithms produce sequences which are uniformly distributed by any of several tests" (Pseudorandom number generator - Wikipedia, the free encyclopedia). So we may assume that the function rand(), will generate a uniform distribution of pseudorandom numbers. And according to the man pages (section 3): "The rand() function returns a pseudo-random integer between 0 and RAND_MAX" (RAND).

Thus, in order to call a function p percent of the time, I would get a value from a random number generator. Then test if it falls within a range of the outputs of the random number generator that represents p percent of it's possible outputs, and invoke the function in question. Otherwise continue execution of the program without invoking the function.

Does that make sense to you?

Best Regards,

New Ink -- Henry
• 09-10-2010
siddhabathula
Hi guys,

Thanks for the help! FYI This is not a homework problem! I want to use it for an embedded systems experiment and I need some good random number generator, where as rand() is not so.

If I do not have a good random number generator than I will not get good results!

Thanks,
Kartik
• 09-10-2010
CommonTater
If you seed the random number generator from a non-repeating value such as "time" -- the number of seconds since Jan1, 1970, I believe-- then use the same sequence throughout your code, it's not too bad... Like this: srand(time(NULL)); I've tested it a little bit and got totally different sequences every time.

Also, if you constrain your choices with modulus you can get almost any integer range you need... for example int x = rand() % 100; will give you a random number from 0 to 99, inclusive.

The probability of any number in your range is about equal for all numbers in your range... so I'd guess that unless you can't bear the same number ever coming up twice, you should be fine.