pitrou commented on a change in pull request #11864: URL: https://github.com/apache/arrow/pull/11864#discussion_r768646227
########## File path: cpp/src/arrow/compute/kernels/random.cc ########## @@ -0,0 +1,63 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <memory> +#include <random> + +#include "arrow/compute/kernel.h" +#include "arrow/compute/kernels/common.h" +#include "arrow/compute/registry.h" +#include "arrow/util/pcg_random.h" + +namespace arrow { +namespace compute { +namespace internal { +namespace { + +// Generates a random floating point number in range [0, 1). +template <class Rng> +double generate_uniform(Rng& rng) { + double range = static_cast<double>(Rng::max() - Rng::min()) + 1; + double value = static_cast<double>(rng() - Rng::min()); + return value / range; +} + +Status ExecRandom(KernelContext* ctx, const ExecBatch& batch, Datum* out) { + static thread_local std::random_device rd; + static thread_local random::pcg64 gen(rd()); Review comment: You're right, we'll need an option to specify the size, which makes this function a bit unusual. The options could look like this: ```c++ class RandomOptions : public FunctionOptions { public: static RandomOptions FromSystemRandom(int64_t length) { return RandomOptions{SystemRandom, length, 0}; } static RandomOptions FromSeed(int64_t length, int64_t seed) { return RandomOptions{Seed, length, seed}; } // Default constructor creates an invalid options struct, but is required for Python RandomOptions() {} enum Initializer { SystemRandom, Seed; }; Initializer initializer = SystemRandom; int64_t length = -1; // -1 means invalid int64_t seed = 0; }; ``` or perhaps: ```c++ class RandomOptions : public FunctionOptions { public: static RandomOptions FromSystemRandom(int64_t length) { return RandomOptions{SystemRandom(), length}; } static RandomOptions FromSeed(int64_t length, int64_t seed) { return RandomOptions{Seed{seed}, length}; } // Default constructor creates an invalid options struct, but is required for Python RandomOptions() {} struct Seed { int64_t seed; }; struct SystemRandom {}; using Initializer = util::variant<SystemRandom, Seed>; Initializer initializer = SystemRandom(); int64_t length = -1; // -1 means invalid }; ``` @bkietz Thoughts? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
