libzmq master
The Intelligent Transport Layer

random.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2011 250bpm s.r.o.
00003     Copyright (c) 2011 Other contributors as noted in the AUTHORS file
00004 
00005     This file is part of 0MQ.
00006 
00007     0MQ is free software; you can redistribute it and/or modify it under
00008     the terms of the GNU Lesser General Public License as published by
00009     the Free Software Foundation; either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     0MQ is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include <stdlib.h>
00022 
00023 #include "platform.hpp"
00024 #if defined ZMQ_HAVE_WINDOWS
00025 #include "windows.hpp"
00026 #else
00027 #include <unistd.h>
00028 #endif
00029 
00030 #include "random.hpp"
00031 #include "stdint.hpp"
00032 #include "clock.hpp"
00033 
00034 void zmq::seed_random ()
00035 {
00036 #if defined ZMQ_HAVE_WINDOWS
00037     int pid = (int) GetCurrentProcessId ();
00038 #else
00039     int pid = (int) getpid ();
00040 #endif
00041     srand ((unsigned int) (clock_t::now_us () + pid));
00042 }
00043 
00044 uint32_t zmq::generate_random ()
00045 {
00046     //  Compensate for the fact that rand() returns signed integer.
00047     uint32_t low = (uint32_t) rand ();
00048     uint32_t high = (uint32_t) rand ();
00049     high <<= (sizeof (int) * 8 - 1);
00050     return high | low;
00051 }
00052 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines