[rtp_rtcp] lint errors about rand() usage fixed.

rand() usage replaced with new Random class, which also makes it clearer what interval random number is in.

BUG=webrtc:5277
R=mflodman

Review URL: https://codereview.webrtc.org/1519503002

Cr-Commit-Position: refs/heads/master@{#11019}
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc b/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc
index ddf24ab..ea62fcb 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_sender.cc
@@ -11,7 +11,6 @@
 #include "webrtc/modules/rtp_rtcp/source/rtcp_sender.h"
 
 #include <assert.h>  // assert
-#include <stdlib.h>  // rand
 #include <string.h>  // memcpy
 
 #include <algorithm>  // min
@@ -141,6 +140,7 @@
     Transport* outgoing_transport)
     : audio_(audio),
       clock_(clock),
+      random_(clock_->TimeInMicroseconds()),
       method_(RtcpMode::kOff),
       transport_(outgoing_transport),
 
@@ -914,15 +914,9 @@
       SetFlag(kRtcpXrDlrrReportBlock, true);
 
     // generate next time to send an RTCP report
-    // seeded from RTP constructor
-    int32_t random = rand() % 1000;
-    int32_t timeToNext = RTCP_INTERVAL_AUDIO_MS;
+    uint32_t minIntervalMs = RTCP_INTERVAL_AUDIO_MS;
 
-    if (audio_) {
-      timeToNext = (RTCP_INTERVAL_AUDIO_MS / 2) +
-                   (RTCP_INTERVAL_AUDIO_MS * random / 1000);
-    } else {
-      uint32_t minIntervalMs = RTCP_INTERVAL_AUDIO_MS;
+    if (!audio_) {
       if (sending_) {
         // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
         uint32_t send_bitrate_kbit = feedback_state.send_bitrate / 1000;
@@ -931,8 +925,11 @@
       }
       if (minIntervalMs > RTCP_INTERVAL_VIDEO_MS)
         minIntervalMs = RTCP_INTERVAL_VIDEO_MS;
-      timeToNext = (minIntervalMs / 2) + (minIntervalMs * random / 1000);
     }
+    // The interval between RTCP packets is varied randomly over the
+    // range [1/2,3/2] times the calculated interval.
+    uint32_t timeToNext =
+        random_.Rand(minIntervalMs * 1 / 2, minIntervalMs * 3 / 2);
     next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + timeToNext;
 
     StatisticianMap statisticians =
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_sender.h b/webrtc/modules/rtp_rtcp/source/rtcp_sender.h
index fcb9012..11a389e 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_sender.h
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_sender.h
@@ -17,6 +17,7 @@
 #include <string>
 #include <vector>
 
+#include "webrtc/base/random.h"
 #include "webrtc/base/scoped_ptr.h"
 #include "webrtc/base/thread_annotations.h"
 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
@@ -202,6 +203,7 @@
  private:
   const bool audio_;
   Clock* const clock_;
+  Random random_ GUARDED_BY(critical_section_rtcp_sender_);
   RtcpMode method_ GUARDED_BY(critical_section_rtcp_sender_);
 
   Transport* const transport_;
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_fec_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtp_fec_unittest.cc
index 541f522..80f961b 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_fec_unittest.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtp_fec_unittest.cc
@@ -11,6 +11,7 @@
 #include <list>
 
 #include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/base/random.h"
 #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h"
 
@@ -41,8 +42,12 @@
 class RtpFecTest : public ::testing::Test {
  protected:
   RtpFecTest()
-      : fec_(new ForwardErrorCorrection()), ssrc_(rand()), fec_seq_num_(0) {}
+      : random_(0xfec133700742),
+        fec_(new ForwardErrorCorrection()),
+        ssrc_(random_.Rand<uint32_t>()),
+        fec_seq_num_(0) {}
 
+  webrtc::Random random_;
   ForwardErrorCorrection* fec_;
   int ssrc_;
   uint16_t fec_seq_num_;
@@ -891,22 +896,20 @@
   assert(num_media_packets > 0);
   ForwardErrorCorrection::Packet* media_packet = NULL;
   int sequence_number = start_seq_num;
-  int time_stamp = rand();
+  int time_stamp = random_.Rand<int>();
 
   for (int i = 0; i < num_media_packets; ++i) {
     media_packet = new ForwardErrorCorrection::Packet;
     media_packet_list_.push_back(media_packet);
-    media_packet->length = static_cast<size_t>(
-        (static_cast<float>(rand()) / RAND_MAX) *
-        (IP_PACKET_SIZE - kRtpHeaderSize - kTransportOverhead -
-         ForwardErrorCorrection::PacketOverhead()));
+    const uint32_t kMinPacketSize = kRtpHeaderSize;
+    const uint32_t kMaxPacketSize = IP_PACKET_SIZE - kRtpHeaderSize -
+                                    kTransportOverhead -
+                                    ForwardErrorCorrection::PacketOverhead();
+    media_packet->length = random_.Rand(kMinPacketSize, kMaxPacketSize);
 
-    if (media_packet->length < kRtpHeaderSize) {
-      media_packet->length = kRtpHeaderSize;
-    }
     // Generate random values for the first 2 bytes
-    media_packet->data[0] = static_cast<uint8_t>(rand() % 256);
-    media_packet->data[1] = static_cast<uint8_t>(rand() % 256);
+    media_packet->data[0] = random_.Rand<uint8_t>();
+    media_packet->data[1] = random_.Rand<uint8_t>();
 
     // The first two bits are assumed to be 10 by the FEC encoder.
     // In fact the FEC decoder will set the two first bits to 10 regardless of
@@ -929,7 +932,7 @@
 
     // Generate random values for payload.
     for (size_t j = 12; j < media_packet->length; ++j) {
-      media_packet->data[j] = static_cast<uint8_t>(rand() % 256);
+      media_packet->data[j] = random_.Rand<uint8_t>();
     }
     sequence_number++;
   }
@@ -940,5 +943,5 @@
 }
 
 int RtpFecTest::ConstructMediaPackets(int num_media_packets) {
-  return ConstructMediaPacketsSeqNum(num_media_packets, rand());
+  return ConstructMediaPacketsSeqNum(num_media_packets, random_.Rand<int>());
 }
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_sender.cc b/webrtc/modules/rtp_rtcp/source/rtp_sender.cc
index 152293a..4e91a29 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_sender.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtp_sender.cc
@@ -34,6 +34,7 @@
 namespace {
 
 const size_t kRtpHeaderLength = 12;
+const uint16_t kMaxInitRtpSeqNumber = 32767;  // 2^15 -1.
 
 const char* FrameTypeToString(FrameType frame_type) {
   switch (frame_type) {
@@ -126,6 +127,7 @@
       // TickTime.
       clock_delta_ms_(clock_->TimeInMilliseconds() -
                       TickTime::MillisecondTimestamp()),
+      random_(clock_->TimeInMicroseconds()),
       bitrates_(new BitrateAggregator(bitrate_callback)),
       total_bitrate_sent_(clock, bitrates_->total_bitrate_observer()),
       audio_configured_(audio),
@@ -183,8 +185,8 @@
   ssrc_rtx_ = ssrc_db_.CreateSSRC();  // Can't be 0.
   bitrates_->set_ssrc(ssrc_);
   // Random start, 16 bits. Can't be 0.
-  sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
-  sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
+  sequence_number_rtx_ = random_.Rand(1, kMaxInitRtpSeqNumber);
+  sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber);
 }
 
 RTPSender::~RTPSender() {
@@ -1656,8 +1658,7 @@
     // Don't initialize seq number if SSRC passed externally.
     if (!sequence_number_forced_ && !ssrc_forced_) {
       // Generate a new sequence number.
-      sequence_number_ =
-          rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER);  // NOLINT
+      sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber);
     }
   }
 }
@@ -1719,8 +1720,7 @@
   ssrc_ = ssrc;
   bitrates_->set_ssrc(ssrc_);
   if (!sequence_number_forced_) {
-    sequence_number_ =
-        rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER);  // NOLINT
+    sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber);
   }
 }
 
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_sender.h b/webrtc/modules/rtp_rtcp/source/rtp_sender.h
index 30ac204..2aa7f4c 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_sender.h
+++ b/webrtc/modules/rtp_rtcp/source/rtp_sender.h
@@ -16,6 +16,7 @@
 #include <utility>
 #include <vector>
 
+#include "webrtc/base/random.h"
 #include "webrtc/base/thread_annotations.h"
 #include "webrtc/common_types.h"
 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
@@ -27,8 +28,6 @@
 #include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
 #include "webrtc/transport.h"
 
-#define MAX_INIT_RTP_SEQ_NUMBER 32767  // 2^15 -1.
-
 namespace webrtc {
 
 class BitrateAggregator;
@@ -387,6 +386,7 @@
 
   Clock* clock_;
   int64_t clock_delta_ms_;
+  Random random_ GUARDED_BY(send_critsect_);
 
   rtc::scoped_ptr<BitrateAggregator> bitrates_;
   Bitrate total_bitrate_sent_;
diff --git a/webrtc/modules/rtp_rtcp/test/testFec/test_fec.cc b/webrtc/modules/rtp_rtcp/test/testFec/test_fec.cc
index 7f52a87..b164b7e 100644
--- a/webrtc/modules/rtp_rtcp/test/testFec/test_fec.cc
+++ b/webrtc/modules/rtp_rtcp/test/testFec/test_fec.cc
@@ -22,10 +22,10 @@
 #include <list>
 
 #include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/base/random.h"
+#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h"
 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.h"
-
-#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
 #include "webrtc/test/testsupport/fileutils.h"
 
 // #define VERBOSE_OUTPUT
@@ -40,30 +40,31 @@
 void ReceivePackets(
     ForwardErrorCorrection::ReceivedPacketList* toDecodeList,
     ForwardErrorCorrection::ReceivedPacketList* receivedPacketList,
-    uint32_t numPacketsToDecode,
+    size_t numPacketsToDecode,
     float reorderRate,
-    float duplicateRate) {
+    float duplicateRate,
+    Random* random) {
   assert(toDecodeList->empty());
   assert(numPacketsToDecode <= receivedPacketList->size());
 
   ForwardErrorCorrection::ReceivedPacketList::iterator it;
-  for (uint32_t i = 0; i < numPacketsToDecode; i++) {
+  for (size_t i = 0; i < numPacketsToDecode; i++) {
     it = receivedPacketList->begin();
     // Reorder packets.
-    float randomVariable = static_cast<float>(rand()) / RAND_MAX;
+    float randomVariable = random->Rand<float>();
     while (randomVariable < reorderRate) {
       ++it;
       if (it == receivedPacketList->end()) {
         --it;
         break;
       }
-      randomVariable = static_cast<float>(rand()) / RAND_MAX;
+      randomVariable = random->Rand<float>();
     }
     ForwardErrorCorrection::ReceivedPacket* receivedPacket = *it;
     toDecodeList->push_back(receivedPacket);
 
     // Duplicate packets.
-    randomVariable = static_cast<float>(rand()) / RAND_MAX;
+    randomVariable = random->Rand<float>();
     while (randomVariable < duplicateRate) {
       ForwardErrorCorrection::ReceivedPacket* duplicatePacket =
           new ForwardErrorCorrection::ReceivedPacket;
@@ -74,7 +75,7 @@
       duplicatePacket->pkt->length = receivedPacket->pkt->length;
 
       toDecodeList->push_back(duplicatePacket);
-      randomVariable = static_cast<float>(rand()) / RAND_MAX;
+      randomVariable = random->Rand<float>();
     }
     receivedPacketList->erase(it);
   }
@@ -125,7 +126,7 @@
   // Seed the random number generator, storing the seed to file in order to
   // reproduce past results.
   const unsigned int randomSeed = static_cast<unsigned int>(time(NULL));
-  srand(randomSeed);
+  Random random(randomSeed);
   std::string filename = webrtc::test::OutputPath() + "randomSeedLog.txt";
   FILE* randomSeedFile = fopen(filename.c_str(), "a");
   fprintf(randomSeedFile, "%u\n", randomSeed);
@@ -133,8 +134,8 @@
   randomSeedFile = NULL;
 
   uint16_t seqNum = 0;
-  uint32_t timeStamp = static_cast<uint32_t>(rand());
-  const uint32_t ssrc = static_cast<uint32_t>(rand());
+  uint32_t timeStamp = random.Rand<uint32_t>();
+  const uint32_t ssrc = random.Rand(1u, 0xfffffffe);
 
   // Loop over the mask types: random and bursty.
   for (int mask_type_idx = 0; mask_type_idx < kNumFecMaskTypes;
@@ -227,16 +228,15 @@
             for (uint32_t i = 0; i < numMediaPackets; ++i) {
               mediaPacket = new ForwardErrorCorrection::Packet;
               mediaPacketList.push_back(mediaPacket);
-              mediaPacket->length = static_cast<size_t>(
-                  (static_cast<float>(rand()) / RAND_MAX) *
-                  (IP_PACKET_SIZE - 12 - 28 -
-                   ForwardErrorCorrection::PacketOverhead()));
-              if (mediaPacket->length < 12) {
-                mediaPacket->length = 12;
-              }
+              const uint32_t kMinPacketSize = 12;
+              const uint32_t kMaxPacketSize = static_cast<uint32_t>(
+                  IP_PACKET_SIZE - 12 - 28 -
+                  ForwardErrorCorrection::PacketOverhead());
+              mediaPacket->length = random.Rand(kMinPacketSize, kMaxPacketSize);
+
               // Generate random values for the first 2 bytes.
-              mediaPacket->data[0] = static_cast<uint8_t>(rand() % 256);
-              mediaPacket->data[1] = static_cast<uint8_t>(rand() % 256);
+              mediaPacket->data[0] = random.Rand<uint8_t>();
+              mediaPacket->data[1] = random.Rand<uint8_t>();
 
               // The first two bits are assumed to be 10 by the
               // FEC encoder. In fact the FEC decoder will set the
@@ -261,7 +261,7 @@
               ByteWriter<uint32_t>::WriteBigEndian(&mediaPacket->data[8], ssrc);
               // Generate random values for payload
               for (size_t j = 12; j < mediaPacket->length; ++j) {
-                mediaPacket->data[j] = static_cast<uint8_t>(rand() % 256);
+                mediaPacket->data[j] = random.Rand<uint8_t>();
               }
               seqNum++;
             }
@@ -284,8 +284,7 @@
             while (mediaPacketListItem != mediaPacketList.end()) {
               mediaPacket = *mediaPacketListItem;
               // We want a value between 0 and 1.
-              const float lossRandomVariable =
-                  (static_cast<float>(rand()) / (RAND_MAX));
+              const float lossRandomVariable = random.Rand<float>();
 
               if (lossRandomVariable >= lossRate[lossRateIdx]) {
                 mediaLossMask[mediaPacketIdx] = 1;
@@ -310,8 +309,7 @@
             uint32_t fecPacketIdx = 0;
             while (fecPacketListItem != fecPacketList.end()) {
               fecPacket = *fecPacketListItem;
-              const float lossRandomVariable =
-                  (static_cast<float>(rand()) / (RAND_MAX));
+              const float lossRandomVariable = random.Rand<float>();
               if (lossRandomVariable >= lossRate[lossRateIdx]) {
                 fecLossMask[fecPacketIdx] = 1;
                 receivedPacket = new ForwardErrorCorrection::ReceivedPacket;
@@ -382,15 +380,11 @@
             // For error-checking frame completion.
             bool fecPacketReceived = false;
             while (!receivedPacketList.empty()) {
-              uint32_t numPacketsToDecode = static_cast<uint32_t>(
-                  (static_cast<float>(rand()) / RAND_MAX) *
-                      receivedPacketList.size() +
-                  0.5);
-              if (numPacketsToDecode < 1) {
-                numPacketsToDecode = 1;
-              }
+              size_t numPacketsToDecode = random.Rand(
+                  1u, static_cast<uint32_t>(receivedPacketList.size()));
               ReceivePackets(&toDecodeList, &receivedPacketList,
-                             numPacketsToDecode, reorderRate, duplicateRate);
+                             numPacketsToDecode, reorderRate, duplicateRate,
+                             &random);
 
               if (fecPacketReceived == false) {
                 ForwardErrorCorrection::ReceivedPacketList::iterator