xref: /llvm-project/libc/src/stdlib/rand.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
1 //===-- Implementation of rand --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/stdlib/rand.h"
10 #include "src/__support/common.h"
11 #include "src/stdlib/rand_util.h"
12 
13 namespace LIBC_NAMESPACE {
14 
15 // An implementation of the xorshift64star pseudo random number generator. This
16 // is a good general purpose generator for most non-cryptographics applications.
17 LLVM_LIBC_FUNCTION(int, rand, (void)) {
18   rand_next ^= rand_next >> 12;
19   rand_next ^= rand_next << 25;
20   rand_next ^= rand_next >> 27;
21   return static_cast<int>((rand_next * 0x2545F4914F6CDD1Dul) >> 32) & RAND_MAX;
22 }
23 
24 } // namespace LIBC_NAMESPACE
25