Pull from FB rev 63ce89e2f2301e6bba44a111cc7d4218022156f6
[folly.git] / folly / Random.cpp
1 /*
2  * Copyright 2012 Facebook, Inc.
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 "folly/Random.h"
18
19 #include <unistd.h>
20 #include <sys/time.h>
21
22 namespace folly {
23
24 uint32_t randomNumberSeed() {
25   struct timeval tv;
26   gettimeofday(&tv, NULL);
27   const uint32_t kPrime1 = 61631;
28   const uint32_t kPrime2 = 64997;
29   const uint32_t kPrime3 = 111857;
30   return kPrime1 * static_cast<uint32_t>(getpid())
31        + kPrime2 * static_cast<uint32_t>(tv.tv_sec)
32        + kPrime3 * static_cast<uint32_t>(tv.tv_usec);
33 }
34
35 }