using random numbers

Here are two functions I am using in a program:

1
2
3
4
5
6
7
8
int roll(int low, int high)
	{
	int range = high - low + 1;
	srand(time(0));
	int result;
	result = rand() % range + low;
	return result;
	}


1
2
3
4
5
6
7
8
bool isFight(int percent)
	{
	int result = roll(0, 99);
	if (percent > result)
		return true;
	else
		return false;
	}


The type of data I want for isFight is a number between 0 and 100 that gives the approximate percent chance of the condition being satisfied. I tested my program using isFight(50), which should be about a 50% chance, but the condition ends up being satisfied far less than 50% of the time. Any suggestions?
How much less than 50%.
Have your ran it more than once.
You might consider another rng. There are a lot on the web.
Only use srand() once at the beginning of your program. Different pseudo-random sequences are not guaranteed to be mutually random. (You keep resetting yours every time you roll().)

But I'll agree with Somelauw: you'll find a much better random number generator on the web.
Last edited on
Topic archived. No new replies allowed.