[Support] Add sys::Process::GetRandomNumber().
authorDaniel Dunbar <daniel@zuster.org>
Sat, 5 May 2012 16:36:20 +0000 (16:36 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Sat, 5 May 2012 16:36:20 +0000 (16:36 +0000)
 - Primitive API, but we rarely have need for random numbers.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156237 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/Process.h
lib/Support/Process.cpp

index d796b7906d378f54d0579e06b7457872d786cebc..91b92788f2ce6140c1fca616f8f0ffb6565e4bd3 100644 (file)
@@ -1,4 +1,4 @@
-//===- llvm/Support/Process.h ------------------------------------*- C++ -*-===//
+//===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -142,6 +142,10 @@ namespace sys {
 
       /// Resets the terminals colors, or returns an escape sequence to do so.
       static const char *ResetColor();
+
+      /// Get the result of a process wide random number generator. The
+      /// generator will be automatically seeded in non-deterministic fashion.
+      static unsigned GetRandomNumber();
     /// @}
   };
 }
index 88ca7c3f220fa3ee1dc7f430ce062242a26f79f1..c65cb454046b2c03f5090ed82d02fadaa7309165 100644 (file)
@@ -22,6 +22,15 @@ using namespace sys;
 //===          independent code.
 //===----------------------------------------------------------------------===//
 
+unsigned llvm::sys::Process::GetRandomNumber() {
+#if defined(HAVE_ARC4RANDOM)
+  return arc4random();
+#else
+  static int x = (::srand(::time(NULL)), 0);
+  return rand();
+#endif
+}
+
 }
 
 // Include the platform-specific parts of this class.