Bull function?

Hello!

I'm trying to make a bulls function! On average I would like it to hit the Bull 70% of the time and the other 30% (miss) between 1 to 20. As you can see I have the basic code, but I'm just learning so don't really understand what each part does.

1
2
3
4
5
6
7
8
9
int Bull() 
{
        int percent = rand()%100; //lower percent is more throws needed
        int r = rand()%100; //lower r is less throws needed
        if(r<percent)        // hit
        return 50;
        else            // miss
        return 1 + rand()%20;
}
int percent = rand()%70;

would that make it 70%, if so what would r need to be? >.<
That would make percent a random number between 0 and 69, inclusive. It doesn't make much sense to make percent a random number, since it is known (i.e. 70).

You need to make percent 70 and r would be good as it is.
Last edited on
Do you mean like this:
int percent = 70;
int r = rand()%100;

What exactly is r ? >.<

Thanks shacktar
It's a... random number between 0 and 99.

If you know that x percent of the time the return value will be 50, then you can simulate that by saying if ( r < x ) return 50 when r is a random number between the values of 1-100 inclusive.
Topic archived. No new replies allowed.