I have a question regarding the random function.
In the program I'm building I have a function where a random number is needed
but I also have to repeat the function 3 times
randomnumber = rand() %3 + 1
but when I repeat the function 3 times, the random number in all 3 cases is exact the same
does anyone know how I can avoid this?
ps: I'm using codeblocks and I included time.h for this function
You may need to post your code - I would guess that you are calling srand() multiple times with the same (or very similar) value.
Besides what Mats said, have a look at this and this.
There is also the possibility that you are not using srand() at all, and it so happens that the first three numbers generated are the same modulo 3.
my problem seems to be solved, I initialised the srand() in a loop, and that seemed to be the problem
Yes, that is definitely a bad thing to do - it is almost always wrong to call srand() more than once in a program - the only time it actually makes sense is when you actually WANT to repeat the same sequence of random numbers for some reason.
Originally Posted by T1m