Skip to content

[C++][Compute] Random kernels for numeric and binary types #47288

Open

Description

@rok

Describe the enhancement requested

In discussion about making PyArrow tests numpy independent it came up we could more random array generation kernels to produce random test data for PyArrow (see RandomArrayGenerator for arrow c++ test data generator). Other libraries that use PyArrow could use such kernels too (e.g. Pandas uses NumPy).

We have a kernel for producing random float64s (PR), but lack kernels for other types.

API-wise we could either expand RandomOptions with parameters for different types and use pyarrow.compute.random kernel or provide new kernels e.g. pyarrow.compute.random_string.

Component(s)

C++

Activity

  1. rok commented on Aug 8, 2025

    @rok
    MemberAuthor
  2. lidavidm commented on Aug 10, 2025

    @lidavidm
    Member

    Seems reasonable to me. Separate kernels might be cleaner (PyArrow could provide a wrapper)?

  3. pitrou commented on Aug 19, 2025

    @pitrou
    Member

    Copying part of the comment I posted on the PyArrow side:

    Generating random data for testing (where you generally want data to be "interesting" but you don't need high statistical quality) is not the same thing as providing random generation facilities for production (where you generally want data to have guaranteed statistical properties, such as: uniform/normal/etc., with certain parameters).

    We can definitely provide more random generation kernels for other numeric types, but you can also generate float64 and cast to the target type (the exception being higher-precision data types such as decimals).

    As for random binary types, are there well-known distributions we can expose?

  4. rok commented on Aug 19, 2025

    @rok
    MemberAuthor

    Generating random data for testing (where you generally want data to be "interesting" but you don't need high statistical quality) is not the same thing as providing random generation facilities for production (where you generally want data to have guaranteed statistical properties, such as: uniform/normal/etc., with certain parameters).

    Agreed. The reason I've started this discussion is because we already have a random kernel which we claim is uniform. So I assumed we're now in business of statistically exact random generation too.

    We can definitely provide more random generation kernels for other numeric types, but you can also generate float64 and cast to the target type (the exception being higher-precision data types such as decimals).

    Does that preserve statistical qualities?

    As for random binary types, are there well-known distributions we can expose?

    I'm not familiar with those. If we agree there's interest we should discuss case by case I suppose.

  5. pitrou commented on Aug 19, 2025

    @pitrou
    Member

    Generating random data for testing (where you generally want data to be "interesting" but you don't need high statistical quality) is not the same thing as providing random generation facilities for production (where you generally want data to have guaranteed statistical properties, such as: uniform/normal/etc., with certain parameters).

    Agreed. The reason I've started this discussion is because we already have a random kernel which we claim is uniform. So I assumed we're now in business of statistically exact random generation too.

    Kind of, but providing one fundamental building block (the uniform float64 kernel) doesn't mean we should provide all useful derived distributions :) I think this should really depend on users' needs for such kernels, and the (im)practicality of using casts etc.

    We can definitely provide more random generation kernels for other numeric types, but you can also generate float64 and cast to the target type (the exception being higher-precision data types such as decimals).

    Does that preserve statistical qualities?

    I'm not entirely sure (perhaps a mathematician can chime in? @AlenkaF ?), but intuitively it should.

    As for random binary types, are there well-known distributions we can expose?

    I'm not familiar with those. If we agree there's interest we should discuss case by case I suppose.

    Agreed.

  6. rok commented on Aug 19, 2025

    @rok
    MemberAuthor

    Would be interesting to know more from mathematicians indeed :)

    If we can solve the testing needs with the current random uniform kernel (and hopefully in Python) this can be a placeholder for potential other random kernel needs.

    Quickly looking at interesting kernels to have:

    • random integer array
    • random choice from sample
    • random bytes of given length
    • shuffle array
    • permute array
    • sample from various distributions (see numpy)
  7. pitrou commented on Aug 19, 2025

    @pitrou
    Member

    The testing needs should be better solved by RandomArrayGenerator, as I've argued elsewhere :)

  8. lidavidm commented on Aug 20, 2025

    @lidavidm
    Member

    Perhaps another approach would be to provide utilities to work with Hypothesis?

  9. AlenkaF commented on Aug 20, 2025

    @AlenkaF
    Member

    I am happy to do more research on this, but I think that casting floats from higher to lower precision should be fine. However, casting from float to int should involve some kind of scaling to preserve the uniform distribution of the random number generator.

    Something similar to what is discussed here: https://stackoverflow.com/questions/5692189/standard-uniform-dist-to-discrete-uniform-a-b

  10. dangotbanned commented on Dec 1, 2025

    @dangotbanned
    Contributor

    We'd have a use for this in https://github.com/narwhals-dev/narwhals

    ArrowSeries.sample currently depends on numpy to provide the same functionality found in polars.Series.sample.
    pandas.Series.sample has a pretty similar api too

    Example

    from __future__ import annotations
    
    from typing import Any
    import pyarrow as pa
    
    
    def sample(
        arr: pa.ChunkedArray[Any],
        n: int | None,
        *,
        fraction: float | None,
        with_replacement: bool,
        seed: int | None,
    ) -> pa.ChunkedArray[Any]:
        import numpy as np
    
        num_rows = len(arr)
        if n is None and fraction is not None:
            n = int(num_rows * fraction)
        rng = np.random.default_rng(seed=seed)
        idx = np.arange(num_rows)
        mask = rng.choice(idx, size=n, replace=with_replacement)
        return arr.take(mask)
    
    
    ca = pa.chunked_array([[2, 4, 8, 0]])
    result = sample(ca, n=10, with_replacement=True, fraction=None, seed=None)
    print(result.to_pylist())
    [8, 4, 4, 8, 0, 0, 8, 4, 4, 8]
    

    Tbh we should probably be using pyarrow.arange now anyway 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions