]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/cpp/src/gandiva/random_generator_holder.h
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / gandiva / random_generator_holder.h
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#pragma once
19
20#include <memory>
21#include <random>
22
23#include "arrow/status.h"
24#include "arrow/util/io_util.h"
25
26#include "gandiva/function_holder.h"
27#include "gandiva/node.h"
28#include "gandiva/visibility.h"
29
30namespace gandiva {
31
32/// Function Holder for 'random'
33class GANDIVA_EXPORT RandomGeneratorHolder : public FunctionHolder {
34 public:
35 ~RandomGeneratorHolder() override = default;
36
37 static Status Make(const FunctionNode& node,
38 std::shared_ptr<RandomGeneratorHolder>* holder);
39
40 double operator()() { return distribution_(generator_); }
41
42 private:
43 explicit RandomGeneratorHolder(int seed) : distribution_(0, 1) {
44 int64_t seed64 = static_cast<int64_t>(seed);
45 seed64 = (seed64 ^ 0x00000005DEECE66D) & 0x0000ffffffffffff;
46 generator_.seed(static_cast<uint64_t>(seed64));
47 }
48
49 RandomGeneratorHolder() : distribution_(0, 1) {
50 generator_.seed(::arrow::internal::GetRandomSeed());
51 }
52
53 std::mt19937_64 generator_;
54 std::uniform_real_distribution<> distribution_;
55};
56
57} // namespace gandiva