You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/12/14 15:53:12 UTC

[GitHub] [arrow] westonpace commented on a change in pull request #11864: ARROW-12404: [C++] Implement "random" nullary function that generates uniform random between 0 and 1

westonpace commented on a change in pull request #11864:
URL: https://github.com/apache/arrow/pull/11864#discussion_r768804558



##########
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:
       It will a bit tricky how we plug these kinds of generation functions into an exec plan.  We will want  the size of the input batch to be mapped into the function options.  Everything we have today funnels through `arrow::compute::ExecuteScalarExpression` in `expression.cc`.  If we assume all nillary functions use some set of common options (we can tackle this when there is more than one nillary function) that might be enough.  We could just code something like...
   
   ```
     auto kernel = call->kernel;
     auto descrs = GetDescriptors(arguments);
     auto options = call->options.get();
     // -- new --
     if (arguments.size() == 0) {
       auto nillary_options = std::dynamic_pointer_cast<NillaryOptions>(options);
       nillary_options.size = input.length;
     }
   ```




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org