generating random number with start num

I am trying to generate random ints from a LOW number and a HIGH number, it does seem to work, but it seems to generate the same number a few times before it moves to another number.

What might i do to improve this? Or is this the best i could do with generating random ints from start/stop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int randnum(int maxnum, int start=0, int increment=0){
	/*maxnum = max number minus one
	 * start = number no less than accepted
	 * increment = add increment to 
	 */
	int num=0;
	srand(time(0)); 
	while (true){
		
		num = (rand() % maxnum) + increment;
		if (start == 0)
			return num;
		else if (num < start)
			continue;
		else
			break;
	}
	return num;
}

int main(){
	cout << randnum(11, 5) << endl;
}


PS i have been doing Python for a while and decided to get back into relearning C++.
Last edited on
move line 13 to right after main()

1
2
3
4
5
int main()
{
  srand(time(0));
  cout << randnum(11, 5) << endl;
}
Last edited on
ah yes, thanks much
Topic archived. No new replies allowed.