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



##########
File path: cpp/src/arrow/compute/kernels/random.cc
##########
@@ -0,0 +1,85 @@
+// 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/builder.h"
+#include "arrow/compute/api_scalar.h"
+#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).
+double generate_uniform(random::pcg64& rng) {
+  return (rng() >> 11) * (1.0 / 9007199254740992.0);
+}

Review comment:
       We can add some checks and explanations here in case the code rotten.
   ```
   // This equation is copied from numpy. It calculates `rng() / 2^64` and
   // the return value is strictly less than 1.
   static_assert(random::pcg64_fast::min() == 0ULL, "");
   static_assert(random::pcg64_fast::max() == ~0ULL, "");
   return (rng() >> 11) * (1.0 / 9007199254740992.0);
   ......
   ```

##########
File path: cpp/src/arrow/compute/kernels/random.cc
##########
@@ -0,0 +1,85 @@
+// 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/builder.h"
+#include "arrow/compute/api_scalar.h"
+#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).
+double generate_uniform(random::pcg64& rng) {
+  return (rng() >> 11) * (1.0 / 9007199254740992.0);
+}
+
+using RandomState = OptionsWrapper<RandomOptions>;
+
+Status ExecRandom(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+  static thread_local std::random_device rd;
+  static thread_local random::pcg64 gen;
+  const RandomOptions& options = RandomState::Get(ctx);
+  DoubleBuilder builder(ctx->memory_pool());
+  if (options.length < 0) {
+    return Status::Invalid("Negative number of elements");
+  }
+  RETURN_NOT_OK(builder.Reserve(options.length));
+  if (options.initializer == RandomOptions::Seed) {
+    gen.seed(options.seed);
+  } else {
+    gen.seed(rd());
+  }
+  for (int i = 0; i < options.length; ++i) {
+    builder.UnsafeAppend(generate_uniform(gen));
+  }
+  std::shared_ptr<Array> double_array;
+  RETURN_NOT_OK(builder.Finish(&double_array));
+  *out = *double_array->data();
+  return Status::OK();
+}
+
+const FunctionDoc random_doc{
+    "Generates a number between 0 and 1",
+    "Generates an uniformly-distributed double-precision number between 0 and 
1.",

Review comment:
       Be more explicit that 0 is inclusive and 1 is exclusive.
   Or simply `Generate a number in [0, 1)`?

##########
File path: cpp/src/arrow/compute/kernels/scalar_random_test.cc
##########
@@ -0,0 +1,68 @@
+// 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 <gtest/gtest.h>
+
+#include <cstdlib>
+
+#include "arrow/compute/api.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/util/checked_cast.h"
+
+namespace arrow {
+namespace compute {
+namespace {
+
+void TestRandomWithOptions(const RandomOptions& random_options) {
+  using arrow::internal::checked_cast;
+  double sum = 0;
+  double square_sum = 0;
+  ASSERT_OK_AND_ASSIGN(Datum result, CallFunction("random", {}, 
&random_options));
+  auto result_array = result.make_array();
+  ASSERT_EQ(result_array->length(), random_options.length);
+  for (int i = 0; i < random_options.length; ++i) {
+    ASSERT_OK_AND_ASSIGN(auto result_scalar, result_array->GetScalar(i));
+    const auto& double_scalar = checked_cast<const 
DoubleScalar&>(*result_scalar);
+    ASSERT_GE(double_scalar.value, 0);
+    ASSERT_LT(double_scalar.value, 1);
+    sum += double_scalar.value;
+    square_sum += double_scalar.value * double_scalar.value;
+  }
+
+  // verify E(X), E(X^2) is near theory
+  const double E_X = 0.5;
+  const double E_X2 = 1.0 / 12 + E_X * E_X;
+  ASSERT_NEAR(sum / random_options.length, E_X, std::abs(E_X) * 0.02);
+  ASSERT_NEAR(square_sum / random_options.length, E_X2, E_X2 * 0.02);
+}
+
+}  // namespace
+
+TEST(TestRandom, Seed) {

Review comment:
       Add a test to verify same seed produces same output?

##########
File path: cpp/src/arrow/compute/kernels/random.cc
##########
@@ -0,0 +1,85 @@
+// 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/builder.h"
+#include "arrow/compute/api_scalar.h"
+#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).
+double generate_uniform(random::pcg64& rng) {
+  return (rng() >> 11) * (1.0 / 9007199254740992.0);
+}
+
+using RandomState = OptionsWrapper<RandomOptions>;
+
+Status ExecRandom(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+  static thread_local std::random_device rd;
+  static thread_local random::pcg64 gen;

Review comment:
       Normal stack variable is okay, without `static thread_local` IMO.
   Shall we use `random::pcg64_fast` instead?

##########
File path: cpp/src/arrow/compute/registry.cc
##########
@@ -188,6 +188,9 @@ static std::unique_ptr<FunctionRegistry> 
CreateBuiltInRegistry() {
   RegisterScalarAggregateTDigest(registry.get());
   RegisterScalarAggregateVariance(registry.get());
 
+  // Random generator function
+  RegisterRandom(registry.get());
+
   RegisterAggregateOptions(registry.get());

Review comment:
       Move RegisterRandom after RegisterAggregateOptions.




-- 
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]


Reply via email to