You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C++][Compute] Random kernels for numeric and binary types #47288
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.
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?
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.
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.
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.
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.
Activity
rok commentedon Aug 8, 2025
@pitrou @lidavidm @raulcd
lidavidm commentedon Aug 10, 2025
Seems reasonable to me. Separate kernels might be cleaner (PyArrow could provide a wrapper)?
pitrou commentedon Aug 19, 2025
Copying part of the comment I posted on the PyArrow side:
We can definitely provide more random generation kernels for other numeric types, but you can also generate
float64and 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?
rok commentedon Aug 19, 2025
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.
Does that preserve statistical qualities?
I'm not familiar with those. If we agree there's interest we should discuss case by case I suppose.
pitrou commentedon Aug 19, 2025
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.
I'm not entirely sure (perhaps a mathematician can chime in? @AlenkaF ?), but intuitively it should.
Agreed.
rok commentedon Aug 19, 2025
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:
pitrou commentedon Aug 19, 2025
The testing needs should be better solved by
RandomArrayGenerator, as I've argued elsewhere :)lidavidm commentedon Aug 20, 2025
Perhaps another approach would be to provide utilities to work with Hypothesis?
AlenkaF commentedon Aug 20, 2025
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
dangotbanned commentedon Dec 1, 2025
We'd have a use for this in https://github.com/narwhals-dev/narwhals
ArrowSeries.samplecurrently depends onnumpyto provide the same functionality found inpolars.Series.sample.pandas.Series.samplehas a pretty similar api tooExample
Tbh we should probably be using
pyarrow.arangenow anyway 😄