summaryrefslogtreecommitdiff
path: root/src/random.cpp
diff options
context:
space:
mode:
authorMartin Sustrik <sustrik@250bpm.com>2011-07-15 11:24:33 +0200
committerMartin Sustrik <sustrik@250bpm.com>2011-07-15 11:24:33 +0200
commitc8e8f2a24cd339c548e06f75a3cef96454671a85 (patch)
treeb8ea021d0755acedca74563cfc74921634071f83 /src/random.cpp
parentba67eff167e94105b0975166a2192060ab125e58 (diff)
ZMQ_IDENTITY socket option removed
This patch simplifies the whole codebase significantly, including dropping depedency on libuuid. Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
Diffstat (limited to 'src/random.cpp')
-rw-r--r--src/random.cpp42
1 files changed, 27 insertions, 15 deletions
diff --git a/src/random.cpp b/src/random.cpp
index 2a1d7d6..9f7768c 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -18,23 +18,35 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <stdlib.h>
+
+#include "platform.hpp"
+#if defined ZMQ_HAVE_WINDOWS
+#include "windows.hpp"
+#else
+#include <unistd.h>
+#endif
+
#include "random.hpp"
#include "stdint.hpp"
-#include "uuid.hpp"
-#include "err.hpp"
+#include "clock.hpp"
+
+void zmq::seed_random ()
+{
+#if defined ZMQ_HAVE_WINDOWS
+ int pid = (int) GetCurrentProcessId ();
+#else
+ int pid = (int) getpid ();
+#endif
+ srand ((unsigned int) (clock_t::now_us () + pid));
+}
-// Here we can use different ways of generating random data, as avialable
-// on different platforms. At the moment, we'll assume the UUID is random
-// enough to use for that purpose.
-void zmq::generate_random (void *buf_, size_t size_)
+uint32_t zmq::generate_random ()
{
- // Collapsing an UUID into 4 bytes.
- zmq_assert (size_ == 4);
- uint32_t buff [4];
- generate_uuid ((void*) buff);
- uint32_t result = buff [0];
- result ^= buff [1];
- result ^= buff [2];
- result ^= buff [3];
- *((uint32_t*) buf_) = result;
+ // Compensate for the fact that rand() returns signed integer.
+ uint32_t low = (uint32_t) rand ();
+ uint32_t high = (uint32_t) rand ();
+ high <<= (sizeof (int) * 8 - 1);
+ return high | low;
}
+