r"""Building the required library in this example requires a source distributionof NumPy or clone of the NumPy git repository since distributions.c is notincluded in binary distributions.On *nix, execute in numpy/random/src/distributionsexport ${PYTHON_VERSION}=3.8 # Python versionexport PYTHON_INCLUDE=#path to Python's include folder, usually \ ${PYTHON_HOME}/include/python${PYTHON_VERSION}mexport NUMPY_INCLUDE=#path to numpy's include folder, usually \ ${PYTHON_HOME}/lib/python${PYTHON_VERSION}/site-packages/numpy/_core/includegcc -shared -o libdistributions.so -fPIC distributions.c \ -I${NUMPY_INCLUDE} -I${PYTHON_INCLUDE}mv libdistributions.so ../../_examples/numba/On Windowsrem PYTHON_HOME and PYTHON_VERSION are setup dependent, this is an exampleset PYTHON_HOME=c:\Anacondaset PYTHON_VERSION=38cl.exe /LD .\distributions.c -DDLL_EXPORT \ -I%PYTHON_HOME%\lib\site-packages\numpy\_core\include \ -I%PYTHON_HOME%\include %PYTHON_HOME%\libs\python%PYTHON_VERSION%.libmove distributions.dll ../../_examples/numba/"""importosimportnumbaasnbimportnumpyasnpfromcffiimportFFIfromnumpy.randomimportPCG64ffi=FFI()ifos.path.exists('./distributions.dll'):lib=ffi.dlopen('./distributions.dll')elifos.path.exists('./libdistributions.so'):lib=ffi.dlopen('./libdistributions.so')else:raiseRuntimeError('Required DLL/so file was not found.')ffi.cdef("""double random_standard_normal(void *bitgen_state);""")x=PCG64()xffi=x.cffibit_generator=xffi.bit_generatorrandom_standard_normal=lib.random_standard_normaldefnormals(n,bit_generator):out=np.empty(n)foriinrange(n):out[i]=random_standard_normal(bit_generator)returnoutnormalsj=nb.jit(normals,nopython=True)# Numba requires a memory address for void *# Can also get address from x.ctypes.bit_generator.valuebit_generator_address=int(ffi.cast('uintptr_t',bit_generator))norm=normalsj(1000,bit_generator_address)print(norm[:12])