say i wanted to do something at random about 20 percent of the time.
the best way i figure is to pick a random number between 0 and 100. pick a range between
0 and 100. the difference in the range is the percent. 20 and 40 is 20 percent;
if the random number is within this range, then "this is the 20% percent of the time".
because what i just typed doesn't make much sense, say i was creating a texas hold'em poker game for example. typically, most players raise before the flop with Ace-King. But to throw people off, some players don't do this and just call. I want to 'just call' about 20 percent of the time and at random. I want to figure out when "20 percent of the time" is.
(btw, i'm not creating a poker game. i've just been thinking about it. that's a little out of my league right now)
1. do you agree with the logic?
2. is there a more elegant way to do this?
3. should this be in the AI section?
Code:void main() { if( between( GetRand(0, 100), 1, 20 ) ) dontRaise(); return void; // :-P } bool between(int n, int x, int y) { int temp; //min-max if(x > y) { temp = x; x = y; y = temp; } if(n >= x && n <= y) return true; return false; } //taken from FAQ int GetRand(int min, int max) { static int Init = 0; int rc; if (Init == 0) { srand(time(NULL)); Init = 1; } rc = (rand() % (max - min + 1) + min); return (rc); }



LinkBack URL
About LinkBacks



