jobs = list() # # now send the makenums job out to the processors, # and retrieve the results, keep track of the time # tic=time.clock() for i in range(numprocs): the_seed=seeds[i] jobs.append(p.apply_async(makenums, (the_seed,the_size))) # # put the results in a new list # out=[j.get() for j in jobs] p.close() toc=time.clock() print "two processor time is {0:5.4e} seconds".format((toc-tic)) # # repeat this with a single process # tic=time.clock() seed=12345 randomval=randomstate(seed=seed) # # double the size since we only have one processor # the_size=the_size*2. out=randomval.uniform(size=the_size) toc=time.clock() print "one processor time is {0:5.4e} seconds".format((toc-tic))
def makenums(seed,the_size): random=randomstate(seed=seed) return random.uniform(size=the_size)
""" Demonstrate a random number generator using the Day 4 beta example """ import matplotlib.pyplot as plt from numpy.random.mtrand import RandomState as randomstate import numpy as np random1=randomstate(seed=5) size=int(10.e6) beta= 1./3. out=random1.uniform(size=size) xval=-1./beta*np.log( 1. - out) fig1=plt.figure(1) fig1.clf() ax1=fig1.add_subplot(111) ax1.hist(out) fig2=plt.figure(2) fig2.clf() ax2=fig2.add_subplot(111) pdf,bins,patches=ax2.hist(xval,bins=np.arange(0,30.,0.01),normed=True) ax2.set_xlim(0,10) test_norm=np.sum(pdf*np.diff(bins)) output=\ """ Is the pdf normaized? Here is test_norm: {0:5.3f}" """ print output.format((test_norm)) xval_an=np.linspace(0.,10.,300.) an_fun=beta*np.exp(-beta*xval_an)