[Numpy-discussion] random number genration

Sturla Molden sturla at molden.no
Tue Mar 29 09:06:36 EDT 2011


Den 29.03.2011 14:56, skrev Sturla Molden:
> import numpy as np
> def randombits(n, p):
>       b = (np.random.rand(n*8).reshape((n,8))<  p).astype(int)
>       return (b<<  range(8)).sum(axis=1).astype(np.uint8)

n is the number of bytes. If you prefer to count in bits:

def randombits(n, p):
      assert(n%8 == 0)
      b = (np.random.rand(n).reshape((n>>3,8))<  p).astype(int)
      return (b<<  range(8)).sum(axis=1).astype(np.uint8)

Sturla



More information about the NumPy-Discussion mailing list