You have to be careful, as your code is producing overflows, even if you do unsigned
arithmetic.
Probably your int
variable is a 32 bit integer which overflows after the number 2.147.483.647
, and if you consider the worst case of your computation, you'll have 1.234*10.999.999 + 101 ==> 13.573.998.867
, before calculating the modulus operation, and this will lead you to error.
The best thing you can do is to use 64 bit number for this kind of calculation not to overflow, with this sample code (you'll see different results even for your normal positive ones)
$ cat pru.c
#include <stdio.h>
#include <stdint.h>
int main()
{
uint64_t i, r=0;
for (i = 0; i < 50; i++) {
r = (1234*r +101) % (11000000);
printf("%llu\n", r);
}
}
which results in:
$ pru
101
124735
10923091
4094395
3483531
8677355
4856171
8515115
2652011
5581675
1787051
5221035
7757291
2497195
1538731
6794155
1987371
10415915
5239211
8186475
4110251
1049835
8496491
1669995
3773931
4030955
2198571
7036715
4306411
1111275
7313451
4798635
3515691
4362795
4689131
387755
5489771
9377515
10853611
6356075
396651
5467435
3814891
10575595
4284331
6864555
860971
6438315
2880811
1920875
This is correct, as 1.234*10.999.999 + 101 ==> 13.573.998.867
will never overflow a uint64_t
number (this is the maximum result you can have) and will produce correct results.