Solved

# How to determine action when given a percentage of the probability

Posted on 2004-11-24
Medium Priority
311 Views
Here is some sample code:
#include <stdio.h>

int optionA = 1;
int optionB = 1;

#define percent 75

int main()
{
int x = 0;

for (x = 1; x < 1000; x++)
{
if (UNKNOWN EQUATION)
{
optionA++;
}
else
{
optionB++;
}
}

return 0;
}

I need to be able to give the equation a percentage, in the example above it is 75.

That mean that I need optionA to be incremented with a 75% chance of probability and when it is not incremented, I need optionB to be incremented.

It would be eacy for me to use x=1000 and when x hits 750 that optionB will be incremented.  I need it seeded through out the loop.

The main idea here is that each time that the UNKOWN EQUATION is evalueated, no matter if x = 1 or x =10,000, it needs to choose an action based on the percentage of chance given to it.

Any ideas?  is this way to simple and I am just missing it?

Thanks for any help!
0
Question by:hoopsho
• 3
• 3
• 2
• +1

LVL 46

Expert Comment

ID: 12666828
You need a random-function, see srand() and rand() in your manual.
0

Author Comment

ID: 12666944
Ah so you would use a random function to determine the outcome...

Is the seeding then, the important piece?  example,,,  is using time(NULL) just fine for the srand?

Then what would you use as the parameter...  I dont need the code just the psuedo logic.

If I wanted a 75% chance of probability, what type of equation would I use?  pick a number from 1 to 75??  while I understand the rand function, etc. I am just not sure how to impliment the equation in this situation.
0

LVL 46

Assisted Solution

Sjef Bosman earned 400 total points
ID: 12666997
Yes, using time() to initialize srand() will do just fine.

What you need to do with rand is not pick a number in a range. It's a lot easier. Just assume rand() gives random integral values, then just take the value modulo 4. For 0, 1, and 2, you go one way, for 3 you take the other. For more difficult things, you could do the same but in double values.
0

LVL 16

Expert Comment

ID: 12667624
...
...
int optionA = 1;
int optionB = 1;

#define percent 75

int main()
{
int x = 0;

srand( (unsigned)time( NULL ) );
for (x = 1; x < 1000; x++)
{
if (((rand() * 100) / RAND_MAX) <= percent)
{
optionA++;
}
else
{
optionB++;
}
}

return 0;
}

Paul
0

LVL 46

Expert Comment

ID: 12667726
>> I dont need the code just the psuedo logic.
0

LVL 16

Accepted Solution

PaulCaswell earned 600 total points
ID: 12667778

The logic is to seed the random system with the current time (as already discussed). Then, for each iteration, get the rand function to generate a number between 0 and 99 by multiplying by 100 and dividing by RAND_MAX, the maximum number that rand can generate. Then it's a simple comparison with your percent figure.

The bits to watch for are:

1. *100 or *99.
2. < percent or <= percent.

I DO NOT REMEMBER THE ANSWERS TO THESE BITS so it would be a good idea to check them yourself.

Paul
0

Author Comment

ID: 12668252
I had to split the points you both helped me solve my problem.  Thank you both very much!
0

LVL 7

Expert Comment

ID: 12668432
Woah, that was fast help :)

Regarding those bits:

Since stdlib's rand() returns a number between 0 and RAND_MAX, multiplying it by 100 will give you 101 numbers - From 0 to 101. You need to multiply it by 99 - you'll have 100 numbers, between 0 and 99. As for the second bit, you want 75 of those cases to be in favor of the event, say 0-74. Then you could use either <=74 or <75 if the number was integer, and you'd have to use <75 if it was a floating point. (<=74 defines an interval of length 74+x, where x is the width of one single point (0 in theory)).
0

LVL 16

Expert Comment

ID: 12673002
aib,

Thanks for filling in the blanks. I had a feeling I had at least one of these points wrong, sad to see it was BOTH. Oh well, QA would have found it ;).

Paul
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
###### Suggested Courses
Course of the Month5 days, 13 hours left to enroll