[ASTERIXDB-2804][FUN] Use pseudo-random for random(seed)

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
Use pseudo-random for random(seed)

Change-Id: I5716aeca017a12937891f4f7a810118aeb12e16d
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/8925
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp
new file mode 100644
index 0000000..8b0aa23
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+FROM range(1, 3) AS r
+SELECT random(int8("12")) AS i8, random(int16("12")) AS i16, random(int32("12")) AS i32, random(int64("12")) AS i64,
+random(float("12")) AS float, random(double("12")) AS double;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm
new file mode 100644
index 0000000..5b9e73c4
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm
@@ -0,0 +1,3 @@
+{ "i8": 0.26795443606823943, "i16": 0.26795443606823943, "i32": 0.26795443606823943, "i64": 0.26795443606823943, "float": 0.26795443606823943, "double": 0.26795443606823943 }
+{ "i8": 0.4533526797678967, "i16": 0.4533526797678967, "i32": 0.4533526797678967, "i64": 0.4533526797678967, "float": 0.4533526797678967, "double": 0.4533526797678967 }
+{ "i8": 0.38508513586474447, "i16": 0.38508513586474447, "i32": 0.38508513586474447, "i64": 0.38508513586474447, "float": 0.38508513586474447, "double": 0.38508513586474447 }
\ No newline at end of file
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
index 38150f5..0efddeb 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
@@ -84,6 +84,7 @@
     public static final int PARAMETERS_REQUIRED = 49;
     public static final int INVALID_PARAM = 50;
     public static final int INCOMPARABLE_TYPES = 51;
+    public static final int ILLEGAL_STATE = 52;
 
     public static final int UNSUPPORTED_JRE = 100;
 
diff --git a/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties b/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
index a02c50b..1a9afd3 100644
--- a/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
+++ b/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
@@ -86,6 +86,7 @@
 49 = Parameter(s) %1$s must be specified
 50 = Invalid parameter \"%1$s\"
 #51 is used
+52 = Illegal state. %1$s
 
 100 = Unsupported JRE: %1$s
 
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
index 2836eae..bd6bd3d 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
@@ -27,6 +27,7 @@
 import org.apache.asterix.om.functions.IFunctionDescriptor;
 import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.om.types.hierachy.ATypeHierarchy;
 import org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.utils.RandomHelper;
 import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
@@ -81,7 +82,9 @@
                             case BIGINT:
                             case FLOAT:
                             case DOUBLE:
-                                randomHelper.setSeed(bytes, offset + 1, arg0.getLength() - 1);
+                                double seed =
+                                        ATypeHierarchy.getDoubleValue(getIdentifier().getName(), 0, bytes, offset);
+                                randomHelper.setSeed(seed);
                                 randomHelper.nextDouble(resultPointable);
                                 break;
                             default:
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
index 7ed1ce5..784d5b8 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
@@ -20,9 +20,11 @@
 package org.apache.asterix.runtime.evaluators.functions.utils;
 
 import java.io.DataOutput;
-import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
 import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider;
 import org.apache.asterix.om.base.AMutableDouble;
 import org.apache.asterix.om.types.BuiltinType;
@@ -30,44 +32,42 @@
 import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.api.IPointable;
 import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
-import org.apache.hyracks.data.std.util.DataUtils;
-import org.apache.hyracks.data.std.util.GrowableArray;
 
 public final class RandomHelper {
 
-    private final SecureRandom random = new SecureRandom();
+    private final SecureRandom random;
 
-    private final GrowableArray seed;
+    private double seed;
+
+    private boolean isFirst;
 
     private final AMutableDouble aDouble = new AMutableDouble(0);
 
     @SuppressWarnings("rawtypes")
-    private ISerializerDeserializer doubleSerde =
+    private final ISerializerDeserializer doubleSerde =
             SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
 
     private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
     private final DataOutput dataOutput = resultStorage.getDataOutput();
 
-    public RandomHelper(boolean withSeed) {
-        seed = withSeed ? new GrowableArray(8) : null;
+    public RandomHelper(boolean withSeed) throws HyracksDataException {
+        if (withSeed) {
+            try {
+                random = SecureRandom.getInstance("SHA1PRNG");
+            } catch (NoSuchAlgorithmException e) {
+                throw new RuntimeDataException(ErrorCode.ILLEGAL_STATE, "random()");
+            }
+        } else {
+            random = new SecureRandom();
+        }
+        isFirst = true;
     }
 
-    public void setSeed(byte[] bytes, int offset, int length) throws HyracksDataException {
-        if (seed == null) {
-            throw new IllegalStateException();
-        }
-
-        boolean sameSeed =
-                seed.getLength() == length && DataUtils.equalsInRange(seed.getByteArray(), 0, bytes, offset, length);
-
-        if (!sameSeed) {
-            try {
-                seed.reset();
-                seed.append(bytes, offset, length);
-                random.setSeed(seed.getByteArray());
-            } catch (IOException e) {
-                throw HyracksDataException.create(e);
-            }
+    public void setSeed(double seedVal) throws HyracksDataException {
+        if (isFirst || seedVal != seed) {
+            seed = seedVal;
+            isFirst = false;
+            random.setSeed(Double.doubleToLongBits(seedVal));
         }
     }