Solved

Posted on 2004-11-24

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!

#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!

9 Comments

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.

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.

...

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

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.

Sorry for misreading your question.

Paul

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)).

Title | # Comments | Views | Activity |
---|---|---|---|

How to configure Digital Ouput in Renesas RH850/FIL Micrcontroller ? | 5 | 170 | |

c++ substatte a varabe for a string in a LPCTSTR statment | 8 | 68 | |

Arduino EDI - Programming language | 3 | 31 | |

Unable to start eclipse ? | 17 | 48 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**19** Experts available now in Live!