Thread: How to execute a function with a certain probability

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Question 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?

    Thanks in advance,
    Kartik

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    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
    Last edited by new_ink2001; 09-10-2010 at 06:49 PM. Reason: grammer

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    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

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku Brute Force Algorithm Help (recursion?)
    By ridilla in forum C Programming
    Replies: 22
    Last Post: 05-07-2010, 03:31 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

Tags for this Thread