Generate a random number from 1 to 100. If it is less than or equal to x, return true, else return false. This will ensure that ur returning true with x/100 probability.
Cheers Nikhil Jindal On Thu, Jul 28, 2011 at 4:21 PM, KK <kunalkapadi...@gmail.com> wrote: > bool foo(int x) > > // Implement this function where 0 <= x <= 100 > It should return true x% of times n false otherwise.... > > first i told him to have a static int s then increment it each time > the func is called... > and if s % (100 - x ) == 0 then true else false..... > > then he told me to have some different approach.. > > I told him like this: > > bool foo(int x) > { > // checking if x is btw 0 & 100 > > if(x == 0) > return false; > if(x == 100) > return true; > > srand(time(0)); > int rno = rand(); > > if(rno % (100 - x) == 0) > return True; > else > return False; > } > > He was like okk but i think he was not completely satisfied.... > Any other Approach... > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.