somgoozy somgoozy - 1 year ago 79
Python Question

Random Odd Numbers

My professor gave us our first assignment (Cs315), which involves dealing with some huge (odd) integers and determining if they are prime. I started to do this in c++ until I realized that even long long ints could not hold the numbers needed, so I was left with the choices of making a class of vectors in c++, or learning python in a few days. This simple piece of Python code is supposed to spit out an odd 256 bit random number. It spits out random numbers, both even and odd, and I don't know why, but my guess is a that it is a simple syntactical error that I am not seeing.

import random
x = random.getrandbits(256)
if x % 2 == 0:
x + 1
print x

Answer Source

You need to assign x + 1 back to x. You can either do this like this: x = x+1 or like this: x += 1

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download