{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {},
  "_ordered_sections": [],
  "item_file": null,
  "item_line": null,
  "item_type": null,
  "aliases": [],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": null,
  "references": null,
  "qa": "reference:random:extending",
  "arbitrary": [
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "s have been designed to be extendable using standard tools for high-performance Python -- numba and Cython.  The "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "Generator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " object can also be used with user-provided "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "s as long as these export a small set of required functions."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Extending"
        }
      ],
      "level": 0,
      "target": "extending"
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Numba can be used with either "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "CTypes"
                }
              ],
              "url": "https://docs.python.org/3/library/ctypes.html",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " or "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "CFFI"
                }
              ],
              "url": "https://cffi.readthedocs.io/en/stable/overview.html",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". The current iteration of the "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "s all export a small set of functions through both interfaces."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This example shows how Numba can be used to produce Gaussian samples using a pure Python implementation which is then compiled.  The random numbers are provided by "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ctypes.next_double"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Both CTypes and CFFI allow the more complicated distributions to be used directly in Numba after compiling the file distributions.c into a "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "DLL"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " or "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "so"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ".  An example showing the use of a more complicated distribution is in the "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "Examples",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " section below."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Numba"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Cython can be used to unpack the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "PyCapsule"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " provided by a "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". This example uses "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "PCG64",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and the example from above.  The usual caveats for writing high-performance code using Cython -- removing bounds checks and wrap around, providing array alignment information -- still apply."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " can also be directly accessed using the members of the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "bitgen_t"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " struct."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Cython can be used to directly access the functions in "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "numpy/random/c_distributions.pxd"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". This requires linking with the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "npyrandom"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " library located in "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "numpy/random/lib"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "See "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "extending_cython_example",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "reference:random:examples:cython:index"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " for the complete listings of these examples and a minimal "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "setup.py"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to build the c-extension modules."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Cython"
        }
      ],
      "level": 1,
      "target": "random_cython"
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "CFFI can be used to directly access the functions in "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "include/numpy/random/distributions.h"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". Some \"massaging\" of the header file is required:"
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Once the header is parsed by "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ffi.cdef"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", the functions can be accessed directly from the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "_generator"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " shared object, using the "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator.cffi",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " interface."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "CFFI"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "Generator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " can be used with user-provided "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "s. The simplest way to write a new "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is to examine the pyx file of one of the existing "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "s. The key structure that must be provided is the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "capsule"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " which contains a "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "PyCapsule"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to a struct pointer of type "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "bitgen_t"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ","
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "typedef struct bitgen {\n  void *state;\n  uint64_t (*next_uint64)(void *st);\n  uint32_t (*next_uint32)(void *st);\n  double (*next_double)(void *st);\n  uint64_t (*next_raw)(void *st);\n} bitgen_t;",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "which provides 5 pointers. The first is an opaque pointer to the data structure used by the "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BitGenerator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "s.  The next three are function pointers which return the next 64- and 32-bit unsigned integers, the next random double and the next raw value. This final function is used for testing and so can be set to the next 64-bit unsigned integer function if not needed. Functions inside "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "Generator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " use this structure as in"
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "bitgen_state->next_uint64(bitgen_state->state)",
          "execution_status": null
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "New BitGenerators"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "BulletList",
          "__tag": 4053,
          "ordered": false,
          "start": 1,
          "children": [
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "Numba",
                      "reference": {
                        "__type": "LocalRef",
                        "__tag": 4022,
                        "kind": "docs",
                        "path": "reference:random:examples:numba"
                      },
                      "kind": "exists"
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "CFFI + Numba",
                      "reference": {
                        "__type": "LocalRef",
                        "__tag": 4022,
                        "kind": "docs",
                        "path": "reference:random:examples:numba_cffi"
                      },
                      "kind": "exists"
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "Cython",
                      "reference": {
                        "__type": "LocalRef",
                        "__tag": 4022,
                        "kind": "docs",
                        "path": "reference:random:examples:cython:index"
                      },
                      "kind": "exists"
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "CFFI",
                      "reference": {
                        "__type": "LocalRef",
                        "__tag": 4022,
                        "kind": "docs",
                        "path": "reference:random:examples:cffi"
                      },
                      "kind": "exists"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Examples"
        }
      ],
      "level": 1,
      "target": null
    }
  ],
  "local_refs": []
}