1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "vts_datatype.h"
18 
19 #include <stdlib.h>
20 #include <time.h>
21 
22 namespace android {
23 namespace vts {
24 
RandomNumberGeneratorReset()25 void RandomNumberGeneratorReset() { srand(time(NULL)); }
26 
RandomUint32()27 uint32_t RandomUint32() { return (unsigned int)rand(); }
28 
RandomInt32()29 int32_t RandomInt32() { return rand(); }
30 
RandomUint64()31 uint64_t RandomUint64() {
32   uint64_t num = (unsigned int)rand();
33   return (num << 32) | (unsigned int)rand();
34 }
35 
RandomInt64()36 int64_t RandomInt64() {
37   int64_t num = rand();
38   return (num << 32) | rand();
39 }
40 
RandomFloat()41 float RandomFloat() { return (float)rand() / (float)(RAND_MAX / 1000000000.0); }
42 
RandomDouble()43 double RandomDouble() {
44   return (double)rand() / (double)(RAND_MAX / 1000000000.0);
45 }
46 
RandomBool()47 bool RandomBool() { return (abs(rand()) % 2) == 1; }
48 
RandomCharPointer()49 char* RandomCharPointer() {
50   int len = RandomUint32() % MAX_CHAR_POINTER_LENGTH;
51   char* buf = (char*)malloc(len);
52   buf[len - 1] = '\0';
53   return buf;
54 }
55 
RandomVoidPointer()56 void* RandomVoidPointer() {
57   int len = RandomUint32() % MAX_CHAR_POINTER_LENGTH;
58   void* buf = malloc(len);
59   return buf;
60 }
61 
62 }  // namespace vts
63 }  // namespace android
64