# Integer Overflow

Ursus Horibilis ursus_horibilis at hotmail.com
Wed Nov 28 15:34:43 CET 2001

```"Gareth McCaughan" <Gareth.McCaughan at pobox.com> wrote in
message news:slrna08439.1ra3.Gareth.McCaughan at g.local...
> "Ursus Horibilis" wrote:
>
> > unsigned int Lcprng(unsigned int *seed)
> > {
> >      *seed = 29 * (*seed) + 13;
> >      return (*seed);
> > }
> >
> > How do you do this in Python?
>
> Points to note:
>
>   - using a 1-element list is about the nearest equivalent
>     we have to passing in a pointer as your C code does.
>     It's not necessarily good style (but then, neither is
>     the pointer-passing thing).

Could you expand on that?  It sounds like it has all the
makings of a religious war (;-)  Would the following be a
preferred way of doing things:

def Lcprng(state):
"""Pass this a variable containing the seed. Don't forget to
update the seed with the returned value each time this
function is called."""
return int((29L*state+13L) & 0x0FFFFFFFL)

.......Calling Program......
RanNum = 987654321 # Initialize Random Number Seed

............................................

RanNum = Lcprng(RanNum) # Keep updating seed with value

...........................................

>
>   - After one iteration, the value in "state" will be a
>     long integer and there will therefore be no danger
>     of overflow. But you should make sure you start it
>     off with a long anyway.

What will happen if I do this:

state[0] = int((29L * state[0] + 13L) & 0x0FFFFFFFFL)

?
>
> By the way, that's not a very good random number generator.
:-)

Yes, you're right.  Linear Congruential PRNG's are not very
good, but they are widely used because of their simplicity.  I
just whipped an equation off the top of my head that would
generate maximal-period pseudo random numbers.  I wasn't

Better is:

state = 179418817 + 179419969 * state

```