src/core/model/random-variable-stream.cc
author Mitch Watrous <watrous@u.washington.edu>
Tue, 10 Jul 2012 21:47:16 -0700
changeset 8872 32d1fa5dfb9f
parent 8871 60846d2741c0
child 8892 b5ef4158fb8f
permissions -rw-r--r--
Port remaining RandomVariable implementations to RandomVariableStream
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8871
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
     2
/*
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
     3
 * Copyright (c) 2006 Georgia Tech Research Corporation
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
     4
 * Copyright (c) 2011 Mathieu Lacage
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
     5
 *
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
     8
 * published by the Free Software Foundation;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
     9
 *
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    13
 * GNU General Public License for more details.
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    14
 *
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    18
 *
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    19
 * Authors: Rajib Bhattacharjea<raj.b@gatech.edu>
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    20
 *          Hadi Arbabi<marbabi@cs.odu.edu>
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    21
 *          Mathieu Lacage <mathieu.lacage@gmail.com>
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    22
 *
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    23
 * Modified by Mitch Watrous <watrous@u.washington.edu>
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    24
 *
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    25
 */
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    26
#include "random-variable-stream.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    27
#include "assert.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    28
#include "boolean.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    29
#include "double.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    30
#include "integer.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    31
#include "string.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    32
#include "pointer.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    33
#include "log.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    34
#include "rng-stream.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    35
#include "rng-seed-manager.h"
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    36
#include <cmath>
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    37
#include <iostream>
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    38
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    39
NS_LOG_COMPONENT_DEFINE ("RandomVariableStream");
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    40
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    41
namespace ns3 {
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    42
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    43
NS_OBJECT_ENSURE_REGISTERED (RandomVariableStream);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    44
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    45
TypeId 
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    46
RandomVariableStream::GetTypeId (void)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    47
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    48
  static TypeId tid = TypeId ("ns3::RandomVariableStream")
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    49
    .SetParent<Object> ()
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    50
    .AddAttribute("Stream",
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    51
		  "The stream number for this RNG stream. -1 means \"allocate a stream automatically\". "
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    52
		  "Note that if -1 is set, Get will return -1 so that it is not possible to know which "
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    53
		  "value was automatically allocated.",
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    54
		  IntegerValue(-1),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    55
		  MakeIntegerAccessor(&RandomVariableStream::SetStream,
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    56
				      &RandomVariableStream::GetStream),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    57
		  MakeIntegerChecker<int64_t>())
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    58
    .AddAttribute("Antithetic", "Set this RNG stream to generate antithetic values",
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    59
		  BooleanValue (false),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    60
		  MakeBooleanAccessor(&RandomVariableStream::SetAntithetic,
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    61
				      &RandomVariableStream::IsAntithetic),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    62
		  MakeBooleanChecker())
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    63
    ;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    64
  return tid;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    65
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    66
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    67
RandomVariableStream::RandomVariableStream()
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    68
  : m_rng (0)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    69
{}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    70
RandomVariableStream::~RandomVariableStream()
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    71
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    72
  delete m_rng;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    73
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    74
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    75
void
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    76
RandomVariableStream::SetAntithetic(bool isAntithetic)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    77
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    78
  m_isAntithetic = isAntithetic;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    79
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    80
bool
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    81
RandomVariableStream::IsAntithetic(void) const
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    82
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    83
  return m_isAntithetic;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    84
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    85
void
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    86
RandomVariableStream::SetStream (int64_t stream)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    87
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    88
  NS_LOG_FUNCTION (this << stream);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    89
  // negative values are not legal.
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    90
  NS_ASSERT (stream >= -1);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    91
  delete m_rng;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    92
  if (stream == -1)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    93
    {
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    94
      // The first 2^63 streams are reserved for automatic stream
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    95
      // number assignment.
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    96
      uint64_t nextStream = RngSeedManager::GetNextStreamIndex ();
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    97
      NS_ASSERT(nextStream <= ((1ULL)<<63));
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    98
      m_rng = new RngStream (RngSeedManager::GetSeed (),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
    99
                             nextStream,
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   100
                             RngSeedManager::GetRun ());
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   101
    }
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   102
  else
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   103
    {
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   104
      // The last 2^63 streams are reserved for deterministic stream
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   105
      // number assignment.
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   106
      uint64_t base = ((1ULL)<<63);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   107
      uint64_t target = base + stream;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   108
      m_rng = new RngStream (RngSeedManager::GetSeed (),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   109
                             target,
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   110
                             RngSeedManager::GetRun ());
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   111
    }
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   112
  m_stream = stream;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   113
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   114
int64_t
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   115
RandomVariableStream::GetStream(void) const
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   116
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   117
  return m_stream;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   118
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   119
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   120
RngStream *
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   121
RandomVariableStream::Peek(void) const
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   122
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   123
  return m_rng;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   124
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   125
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   126
NS_OBJECT_ENSURE_REGISTERED(UniformRandomVariable);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   127
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   128
TypeId 
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   129
UniformRandomVariable::GetTypeId (void)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   130
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   131
  static TypeId tid = TypeId ("ns3::UniformRandomVariable")
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   132
    .SetParent<RandomVariableStream>()
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   133
    .AddConstructor<UniformRandomVariable> ()
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   134
    .AddAttribute("Min", "The lower bound on the values returned by this RNG stream.",
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   135
		  DoubleValue(0),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   136
		  MakeDoubleAccessor(&UniformRandomVariable::m_min),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   137
		  MakeDoubleChecker<double>())
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   138
    .AddAttribute("Max", "The upper bound on the values returned by this RNG stream.",
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   139
		  DoubleValue(1.0),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   140
		  MakeDoubleAccessor(&UniformRandomVariable::m_max),
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   141
		  MakeDoubleChecker<double>())
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   142
    ;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   143
  return tid;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   144
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   145
UniformRandomVariable::UniformRandomVariable ()
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   146
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   147
  // m_min and m_max are initialized after constructor by attributes
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   148
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   149
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   150
double 
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   151
UniformRandomVariable::GetMin (void) const
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   152
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   153
  return m_min;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   154
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   155
double 
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   156
UniformRandomVariable::GetMax (void) const
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   157
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   158
  return m_max;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   159
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   160
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   161
double 
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   162
UniformRandomVariable::GetValue (double min, double max)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   163
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   164
  double v = min + Peek ()->RandU01 () * (max - min);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   165
  if (IsAntithetic ())
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   166
    {
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   167
      v = min + (max - v);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   168
    }
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   169
  return v;
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   170
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   171
uint32_t 
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   172
UniformRandomVariable::GetInteger (uint32_t min, uint32_t max)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   173
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   174
  NS_ASSERT (min <= max);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   175
  return static_cast<uint32_t> ( GetValue (min, max + 1) );
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   176
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   177
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   178
double 
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   179
UniformRandomVariable::GetValue (void)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   180
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   181
  return GetValue (m_min, m_max);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   182
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   183
uint32_t 
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   184
UniformRandomVariable::GetInteger (void)
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   185
{
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   186
  return (uint32_t)GetValue (m_min, m_max + 1);
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   187
}
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
   188
8872
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   189
NS_OBJECT_ENSURE_REGISTERED(ConstantRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   190
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   191
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   192
ConstantRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   193
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   194
  static TypeId tid = TypeId ("ns3::ConstantRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   195
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   196
    .AddConstructor<ConstantRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   197
    .AddAttribute("Constant", "The constant value returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   198
		  DoubleValue(0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   199
		  MakeDoubleAccessor(&ConstantRandomVariable::m_constant),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   200
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   201
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   202
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   203
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   204
ConstantRandomVariable::ConstantRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   205
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   206
  // m_constant is initialized after constructor by attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   207
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   208
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   209
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   210
ConstantRandomVariable::GetConstant (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   211
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   212
  return m_constant;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   213
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   214
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   215
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   216
ConstantRandomVariable::GetValue (double constant)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   217
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   218
  return constant;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   219
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   220
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   221
ConstantRandomVariable::GetInteger (uint32_t constant)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   222
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   223
  return constant;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   224
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   225
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   226
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   227
ConstantRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   228
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   229
  return GetValue (m_constant);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   230
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   231
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   232
ConstantRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   233
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   234
  return (uint32_t)GetValue (m_constant);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   235
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   236
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   237
NS_OBJECT_ENSURE_REGISTERED(SequentialRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   238
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   239
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   240
SequentialRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   241
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   242
  static TypeId tid = TypeId ("ns3::SequentialRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   243
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   244
    .AddConstructor<SequentialRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   245
    .AddAttribute("Min", "The first value of the sequence.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   246
		  DoubleValue(0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   247
		  MakeDoubleAccessor(&SequentialRandomVariable::m_min),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   248
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   249
    .AddAttribute("Max", "One more than the last value of the sequence.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   250
		  DoubleValue(0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   251
		  MakeDoubleAccessor(&SequentialRandomVariable::m_max),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   252
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   253
    .AddAttribute("Increment", "The sequence random variable increment.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   254
                  StringValue("ns3::ConstantRandomVariable[Contant=1]"),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   255
                  MakePointerAccessor (&SequentialRandomVariable::m_increment),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   256
                  MakePointerChecker<RandomVariableStream> ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   257
    .AddAttribute("Consecutive", "The number of times each member of the sequence is repeated.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   258
                  IntegerValue(1),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   259
                  MakeIntegerAccessor(&SequentialRandomVariable::m_consecutive),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   260
                  MakeIntegerChecker<uint32_t>());
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   261
     ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   262
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   263
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   264
SequentialRandomVariable::SequentialRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   265
    :
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   266
    m_current            (0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   267
    m_currentConsecutive (0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   268
    m_isCurrentSet       (false)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   269
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   270
  // m_min, m_max, m_increment, and m_consecutive are initialized
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   271
  // after constructor by attributes.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   272
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   273
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   274
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   275
SequentialRandomVariable::GetMin (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   276
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   277
  return m_min;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   278
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   279
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   280
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   281
SequentialRandomVariable::GetMax (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   282
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   283
  return m_max;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   284
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   285
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   286
Ptr<RandomVariableStream> 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   287
SequentialRandomVariable::GetIncrement (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   288
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   289
  return m_increment;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   290
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   291
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   292
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   293
SequentialRandomVariable::GetConsecutive (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   294
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   295
  return m_consecutive;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   296
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   297
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   298
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   299
SequentialRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   300
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   301
  // Set the current sequence value if it hasn't been set.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   302
  if (!m_isCurrentSet)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   303
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   304
      // Start the sequence at its minimium value.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   305
      m_current = m_min;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   306
      m_isCurrentSet = true;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   307
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   308
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   309
 // Return a sequential series of values
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   310
  double r = m_current;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   311
  if (++m_currentConsecutive == m_consecutive)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   312
    { // Time to advance to next
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   313
      m_currentConsecutive = 0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   314
      m_current += m_increment->GetValue ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   315
      if (m_current >= m_max)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   316
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   317
          m_current = m_min + (m_current - m_max);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   318
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   319
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   320
  return r;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   321
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   322
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   323
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   324
SequentialRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   325
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   326
  return (uint32_t)GetValue ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   327
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   328
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   329
NS_OBJECT_ENSURE_REGISTERED(ExponentialRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   330
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   331
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   332
ExponentialRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   333
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   334
  static TypeId tid = TypeId ("ns3::ExponentialRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   335
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   336
    .AddConstructor<ExponentialRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   337
    .AddAttribute("Mean", "The mean of the values returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   338
		  DoubleValue(1.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   339
		  MakeDoubleAccessor(&ExponentialRandomVariable::m_mean),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   340
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   341
    .AddAttribute("Bound", "The upper bound on the values returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   342
		  DoubleValue(0.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   343
		  MakeDoubleAccessor(&ExponentialRandomVariable::m_bound),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   344
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   345
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   346
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   347
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   348
ExponentialRandomVariable::ExponentialRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   349
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   350
  // m_mean and m_bound are initialized after constructor by attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   351
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   352
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   353
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   354
ExponentialRandomVariable::GetMean (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   355
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   356
  return m_mean;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   357
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   358
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   359
ExponentialRandomVariable::GetBound (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   360
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   361
  return m_bound;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   362
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   363
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   364
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   365
ExponentialRandomVariable::GetValue (double mean, double bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   366
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   367
  while (1)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   368
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   369
      // Get a uniform random variable in [0,1].
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   370
      double v = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   371
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   372
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   373
          v = (1 - v);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   374
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   375
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   376
      // Calculate the exponential random variable.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   377
      double r = -mean*std::log (v);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   378
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   379
      // Use this value if it's acceptable.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   380
      if (bound == 0 || r <= bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   381
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   382
          return r;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   383
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   384
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   385
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   386
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   387
ExponentialRandomVariable::GetInteger (uint32_t mean, uint32_t bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   388
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   389
  return static_cast<uint32_t> ( GetValue (mean, bound) );
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   390
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   391
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   392
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   393
ExponentialRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   394
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   395
  return GetValue (m_mean, m_bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   396
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   397
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   398
ExponentialRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   399
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   400
  return (uint32_t)GetValue (m_mean, m_bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   401
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   402
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   403
NS_OBJECT_ENSURE_REGISTERED(ParetoRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   404
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   405
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   406
ParetoRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   407
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   408
  static TypeId tid = TypeId ("ns3::ParetoRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   409
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   410
    .AddConstructor<ParetoRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   411
    .AddAttribute("Mean", "The mean parameter for the Pareto distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   412
		  DoubleValue(1.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   413
		  MakeDoubleAccessor(&ParetoRandomVariable::m_mean),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   414
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   415
    .AddAttribute("Shape", "The shape parameter for the Pareto distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   416
		  DoubleValue(2.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   417
		  MakeDoubleAccessor(&ParetoRandomVariable::m_shape),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   418
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   419
    .AddAttribute("Bound", "The upper bound on the values returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   420
		  DoubleValue(0.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   421
		  MakeDoubleAccessor(&ParetoRandomVariable::m_bound),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   422
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   423
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   424
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   425
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   426
ParetoRandomVariable::ParetoRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   427
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   428
  // m_mean, m_shape, and m_bound are initialized after constructor
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   429
  // by attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   430
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   431
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   432
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   433
ParetoRandomVariable::GetMean (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   434
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   435
  return m_mean;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   436
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   437
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   438
ParetoRandomVariable::GetShape (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   439
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   440
  return m_shape;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   441
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   442
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   443
ParetoRandomVariable::GetBound (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   444
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   445
  return m_bound;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   446
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   447
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   448
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   449
ParetoRandomVariable::GetValue (double mean, double shape, double bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   450
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   451
  // Calculate the scale parameter.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   452
  double scale = mean * (shape - 1.0) / shape;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   453
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   454
  while (1)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   455
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   456
      // Get a uniform random variable in [0,1].
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   457
      double v = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   458
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   459
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   460
          v = (1 - v);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   461
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   462
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   463
      // Calculate the Pareto random variable.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   464
      double r = (scale * ( 1.0 / std::pow (v, 1.0 / shape)));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   465
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   466
      // Use this value if it's acceptable.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   467
      if (bound == 0 || r <= bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   468
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   469
          return r;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   470
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   471
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   472
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   473
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   474
ParetoRandomVariable::GetInteger (uint32_t mean, uint32_t shape, uint32_t bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   475
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   476
  return static_cast<uint32_t> ( GetValue (mean, shape, bound) );
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   477
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   478
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   479
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   480
ParetoRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   481
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   482
  return GetValue (m_mean, m_shape, m_bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   483
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   484
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   485
ParetoRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   486
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   487
  return (uint32_t)GetValue (m_mean, m_shape, m_bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   488
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   489
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   490
NS_OBJECT_ENSURE_REGISTERED(WeibullRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   491
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   492
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   493
WeibullRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   494
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   495
  static TypeId tid = TypeId ("ns3::WeibullRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   496
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   497
    .AddConstructor<WeibullRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   498
    .AddAttribute("Scale", "The scale parameter for the Weibull distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   499
		  DoubleValue(1.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   500
		  MakeDoubleAccessor(&WeibullRandomVariable::m_scale),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   501
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   502
    .AddAttribute("Shape", "The shape parameter for the Weibull distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   503
		  DoubleValue(1),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   504
		  MakeDoubleAccessor(&WeibullRandomVariable::m_shape),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   505
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   506
    .AddAttribute("Bound", "The upper bound on the values returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   507
		  DoubleValue(0.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   508
		  MakeDoubleAccessor(&WeibullRandomVariable::m_bound),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   509
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   510
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   511
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   512
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   513
WeibullRandomVariable::WeibullRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   514
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   515
  // m_scale, m_shape, and m_bound are initialized after constructor
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   516
  // by attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   517
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   518
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   519
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   520
WeibullRandomVariable::GetScale (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   521
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   522
  return m_scale;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   523
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   524
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   525
WeibullRandomVariable::GetShape (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   526
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   527
  return m_shape;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   528
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   529
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   530
WeibullRandomVariable::GetBound (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   531
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   532
  return m_bound;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   533
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   534
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   535
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   536
WeibullRandomVariable::GetValue (double scale, double shape, double bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   537
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   538
  double exponent = 1.0 / shape;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   539
  while (1)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   540
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   541
      // Get a uniform random variable in [0,1].
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   542
      double v = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   543
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   544
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   545
          v = (1 - v);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   546
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   547
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   548
      // Calculate the Weibull random variable.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   549
      double r = scale * std::pow ( -std::log (v), exponent);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   550
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   551
      // Use this value if it's acceptable.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   552
      if (bound == 0 || r <= bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   553
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   554
          return r;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   555
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   556
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   557
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   558
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   559
WeibullRandomVariable::GetInteger (uint32_t scale, uint32_t shape, uint32_t bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   560
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   561
  return static_cast<uint32_t> ( GetValue (scale, shape, bound) );
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   562
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   563
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   564
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   565
WeibullRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   566
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   567
  return GetValue (m_scale, m_shape, m_bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   568
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   569
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   570
WeibullRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   571
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   572
  return (uint32_t)GetValue (m_scale, m_shape, m_bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   573
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   574
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   575
NS_OBJECT_ENSURE_REGISTERED(NormalRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   576
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   577
const double NormalRandomVariable::INFINITE_VALUE = 1e307;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   578
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   579
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   580
NormalRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   581
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   582
  static TypeId tid = TypeId ("ns3::NormalRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   583
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   584
    .AddConstructor<NormalRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   585
    .AddAttribute("Mean", "The mean value for the normal distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   586
		  DoubleValue(0.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   587
		  MakeDoubleAccessor(&NormalRandomVariable::m_mean),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   588
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   589
    .AddAttribute("Variance", "The variance value for the normal distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   590
		  DoubleValue(1.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   591
		  MakeDoubleAccessor(&NormalRandomVariable::m_variance),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   592
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   593
    .AddAttribute("Bound", "The bound on the values returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   594
		  DoubleValue(INFINITE_VALUE),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   595
		  MakeDoubleAccessor(&NormalRandomVariable::m_bound),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   596
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   597
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   598
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   599
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   600
NormalRandomVariable::NormalRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   601
  :
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   602
  m_nextValid (false)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   603
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   604
  // m_mean, m_variance, and m_bound are initialized after constructor
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   605
  // by attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   606
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   607
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   608
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   609
NormalRandomVariable::GetMean (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   610
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   611
  return m_mean;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   612
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   613
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   614
NormalRandomVariable::GetVariance (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   615
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   616
  return m_variance;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   617
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   618
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   619
NormalRandomVariable::GetBound (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   620
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   621
  return m_bound;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   622
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   623
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   624
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   625
NormalRandomVariable::GetValue (double mean, double variance, double bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   626
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   627
  if (m_nextValid)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   628
    { // use previously generated
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   629
      m_nextValid = false;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   630
      return m_next;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   631
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   632
  while (1)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   633
    { // See Simulation Modeling and Analysis p. 466 (Averill Law)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   634
      // for algorithm; basically a Box-Muller transform:
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   635
      // http://en.wikipedia.org/wiki/Box-Muller_transform
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   636
      double u1 = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   637
      double u2 = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   638
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   639
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   640
          u1 = (1 - u1);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   641
          u2 = (1 - u2);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   642
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   643
      double v1 = 2 * u1 - 1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   644
      double v2 = 2 * u2 - 1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   645
      double w = v1 * v1 + v2 * v2;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   646
      if (w <= 1.0)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   647
        { // Got good pair
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   648
          double y = sqrt ((-2 * log (w)) / w);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   649
          m_next = mean + v2 * y * sqrt (variance);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   650
          // if next is in bounds, it is valid
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   651
          m_nextValid = fabs (m_next - mean) <= bound;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   652
          double x1 = mean + v1 * y * sqrt (variance);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   653
          // if x1 is in bounds, return it
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   654
          if (fabs (x1 - mean) <= bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   655
            {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   656
              return x1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   657
            }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   658
          // otherwise try and return m_next if it is valid
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   659
          else if (m_nextValid)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   660
            {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   661
              m_nextValid = false;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   662
              return m_next;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   663
            }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   664
          // otherwise, just run this loop again
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   665
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   666
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   667
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   668
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   669
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   670
NormalRandomVariable::GetInteger (uint32_t mean, uint32_t variance, uint32_t bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   671
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   672
  return static_cast<uint32_t> ( GetValue (mean, variance, bound) );
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   673
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   674
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   675
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   676
NormalRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   677
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   678
  return GetValue (m_mean, m_variance, m_bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   679
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   680
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   681
NormalRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   682
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   683
  return (uint32_t)GetValue (m_mean, m_variance, m_bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   684
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   685
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   686
NS_OBJECT_ENSURE_REGISTERED(LogNormalRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   687
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   688
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   689
LogNormalRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   690
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   691
  static TypeId tid = TypeId ("ns3::LogNormalRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   692
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   693
    .AddConstructor<LogNormalRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   694
    .AddAttribute("Mu", "The mu value for the log-normal distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   695
		  DoubleValue(0.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   696
		  MakeDoubleAccessor(&LogNormalRandomVariable::m_mu),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   697
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   698
    .AddAttribute("Sigma", "The sigma value for the log-normal distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   699
		  DoubleValue(1.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   700
		  MakeDoubleAccessor(&LogNormalRandomVariable::m_sigma),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   701
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   702
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   703
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   704
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   705
LogNormalRandomVariable::LogNormalRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   706
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   707
  // m_mu and m_sigma are initialized after constructor by
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   708
  // attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   709
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   710
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   711
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   712
LogNormalRandomVariable::GetMu (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   713
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   714
  return m_mu;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   715
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   716
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   717
LogNormalRandomVariable::GetSigma (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   718
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   719
  return m_sigma;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   720
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   721
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   722
// The code from this function was adapted from the GNU Scientific
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   723
// Library 1.8:
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   724
/* randist/lognormal.c
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   725
 *
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   726
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   727
 *
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   728
 * This program is free software; you can redistribute it and/or modify
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   729
 * it under the terms of the GNU General Public License as published by
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   730
 * the Free Software Foundation; either version 2 of the License, or (at
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   731
 * your option) any later version.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   732
 *
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   733
 * This program is distributed in the hope that it will be useful, but
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   734
 * WITHOUT ANY WARRANTY; without even the implied warranty of
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   735
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   736
 * General Public License for more details.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   737
 *
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   738
 * You should have received a copy of the GNU General Public License
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   739
 * along with this program; if not, write to the Free Software
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   740
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   741
 */
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   742
/* The lognormal distribution has the form
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   743
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   744
   p(x) dx = 1/(x * sqrt(2 pi sigma^2)) exp(-(ln(x) - zeta)^2/2 sigma^2) dx
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   745
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   746
   for x > 0. Lognormal random numbers are the exponentials of
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   747
   gaussian random numbers */
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   748
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   749
LogNormalRandomVariable::GetValue (double mu, double sigma)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   750
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   751
  double v1, v2, r2, normal, x;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   752
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   753
  do
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   754
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   755
      /* choose x,y in uniform square (-1,-1) to (+1,+1) */
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   756
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   757
      double u1 = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   758
      double u2 = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   759
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   760
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   761
          u1 = (1 - u1);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   762
          u2 = (1 - u2);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   763
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   764
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   765
      v1 = -1 + 2 * u1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   766
      v2 = -1 + 2 * u2;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   767
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   768
      /* see if it is in the unit circle */
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   769
      r2 = v1 * v1 + v2 * v2;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   770
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   771
  while (r2 > 1.0 || r2 == 0);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   772
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   773
  normal = v1 * std::sqrt (-2.0 * std::log (r2) / r2);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   774
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   775
  x =  std::exp (sigma * normal + mu);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   776
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   777
  return x;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   778
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   779
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   780
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   781
LogNormalRandomVariable::GetInteger (uint32_t mu, uint32_t sigma)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   782
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   783
  return static_cast<uint32_t> ( GetValue (mu, sigma));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   784
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   785
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   786
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   787
LogNormalRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   788
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   789
  return GetValue (m_mu, m_sigma);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   790
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   791
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   792
LogNormalRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   793
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   794
  return (uint32_t)GetValue (m_mu, m_sigma);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   795
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   796
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   797
NS_OBJECT_ENSURE_REGISTERED(GammaRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   798
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   799
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   800
GammaRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   801
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   802
  static TypeId tid = TypeId ("ns3::GammaRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   803
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   804
    .AddConstructor<GammaRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   805
    .AddAttribute("Alpha", "The alpha value for the gamma distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   806
		  DoubleValue(1.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   807
		  MakeDoubleAccessor(&GammaRandomVariable::m_alpha),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   808
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   809
    .AddAttribute("Beta", "The beta value for the gamma distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   810
		  DoubleValue(1.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   811
		  MakeDoubleAccessor(&GammaRandomVariable::m_beta),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   812
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   813
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   814
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   815
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   816
GammaRandomVariable::GammaRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   817
  :
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   818
  m_nextValid (false)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   819
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   820
  // m_alpha and m_beta are initialized after constructor by
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   821
  // attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   822
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   823
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   824
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   825
GammaRandomVariable::GetAlpha (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   826
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   827
  return m_alpha;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   828
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   829
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   830
GammaRandomVariable::GetBeta (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   831
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   832
  return m_beta;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   833
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   834
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   835
/*
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   836
  The code for the following generator functions was adapted from ns-2
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   837
  tools/ranvar.cc
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   838
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   839
  Originally the algorithm was devised by Marsaglia in 2000:
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   840
  G. Marsaglia, W. W. Tsang: A simple method for gereating Gamma variables
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   841
  ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   842
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   843
  The Gamma distribution density function has the form
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   844
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   845
                             x^(alpha-1) * exp(-x/beta)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   846
        p(x; alpha, beta) = ----------------------------
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   847
                             beta^alpha * Gamma(alpha)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   848
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   849
  for x > 0.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   850
*/
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   851
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   852
GammaRandomVariable::GetValue (double alpha, double beta)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   853
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   854
  if (alpha < 1)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   855
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   856
      double u = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   857
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   858
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   859
          u = (1 - u);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   860
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   861
      return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   862
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   863
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   864
  double x, v, u;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   865
  double d = alpha - 1.0 / 3.0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   866
  double c = (1.0 / 3.0) / sqrt (d);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   867
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   868
  while (1)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   869
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   870
      do
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   871
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   872
          // Get a value from a normal distribution that has mean
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   873
          // zero, variance 1, and no bound.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   874
          double mean = 0.0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   875
          double variance = 1.0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   876
          double bound = NormalRandomVariable::INFINITE_VALUE;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   877
          x = GetNormalValue (mean, variance, bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   878
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   879
          v = 1.0 + c * x;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   880
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   881
      while (v <= 0);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   882
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   883
      v = v * v * v;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   884
      u = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   885
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   886
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   887
          u = (1 - u);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   888
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   889
      if (u < 1 - 0.0331 * x * x * x * x)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   890
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   891
          break;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   892
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   893
      if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   894
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   895
          break;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   896
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   897
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   898
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   899
  return beta * d * v;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   900
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   901
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   902
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   903
GammaRandomVariable::GetInteger (uint32_t alpha, uint32_t beta)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   904
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   905
  return static_cast<uint32_t> ( GetValue (alpha, beta));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   906
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   907
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   908
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   909
GammaRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   910
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   911
  return GetValue (m_alpha, m_beta);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   912
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   913
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   914
GammaRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   915
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   916
  return (uint32_t)GetValue (m_alpha, m_beta);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   917
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   918
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   919
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   920
GammaRandomVariable::GetNormalValue (double mean, double variance, double bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   921
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   922
  if (m_nextValid)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   923
    { // use previously generated
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   924
      m_nextValid = false;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   925
      return m_next;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   926
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   927
  while (1)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   928
    { // See Simulation Modeling and Analysis p. 466 (Averill Law)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   929
      // for algorithm; basically a Box-Muller transform:
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   930
      // http://en.wikipedia.org/wiki/Box-Muller_transform
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   931
      double u1 = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   932
      double u2 = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   933
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   934
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   935
          u1 = (1 - u1);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   936
          u2 = (1 - u2);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   937
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   938
      double v1 = 2 * u1 - 1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   939
      double v2 = 2 * u2 - 1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   940
      double w = v1 * v1 + v2 * v2;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   941
      if (w <= 1.0)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   942
        { // Got good pair
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   943
          double y = sqrt ((-2 * log (w)) / w);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   944
          m_next = mean + v2 * y * sqrt (variance);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   945
          // if next is in bounds, it is valid
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   946
          m_nextValid = fabs (m_next - mean) <= bound;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   947
          double x1 = mean + v1 * y * sqrt (variance);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   948
          // if x1 is in bounds, return it
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   949
          if (fabs (x1 - mean) <= bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   950
            {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   951
              return x1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   952
            }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   953
          // otherwise try and return m_next if it is valid
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   954
          else if (m_nextValid)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   955
            {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   956
              m_nextValid = false;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   957
              return m_next;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   958
            }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   959
          // otherwise, just run this loop again
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   960
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   961
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   962
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   963
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   964
NS_OBJECT_ENSURE_REGISTERED(ErlangRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   965
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   966
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   967
ErlangRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   968
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   969
  static TypeId tid = TypeId ("ns3::ErlangRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   970
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   971
    .AddConstructor<ErlangRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   972
    .AddAttribute("K", "The k value for the Erlang distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   973
		  IntegerValue(1),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   974
		  MakeIntegerAccessor(&ErlangRandomVariable::m_k),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   975
		  MakeIntegerChecker<uint32_t>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   976
    .AddAttribute("Lambda", "The lambda value for the Erlang distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   977
		  DoubleValue(1.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   978
		  MakeDoubleAccessor(&ErlangRandomVariable::m_lambda),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   979
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   980
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   981
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   982
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   983
ErlangRandomVariable::ErlangRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   984
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   985
  // m_k and m_lambda are initialized after constructor by attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   986
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   987
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   988
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   989
ErlangRandomVariable::GetK (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   990
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   991
  return m_k;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   992
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   993
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   994
ErlangRandomVariable::GetLambda (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   995
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   996
  return m_lambda;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   997
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   998
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
   999
/*
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1000
  The code for the following generator functions was adapted from ns-2
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1001
  tools/ranvar.cc
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1002
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1003
  The Erlang distribution density function has the form
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1004
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1005
                           x^(k-1) * exp(-x/lambda)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1006
        p(x; k, lambda) = ---------------------------
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1007
                             lambda^k * (k-1)!
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1008
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1009
  for x > 0.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1010
*/
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1011
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1012
ErlangRandomVariable::GetValue (uint32_t k, double lambda)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1013
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1014
  double mean = lambda;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1015
  double bound = 0.0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1016
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1017
  double result = 0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1018
  for (unsigned int i = 0; i < k; ++i)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1019
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1020
      result += GetExponentialValue (mean, bound);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1021
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1022
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1023
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1024
  return result;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1025
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1026
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1027
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1028
ErlangRandomVariable::GetInteger (uint32_t k, uint32_t lambda)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1029
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1030
  return static_cast<uint32_t> ( GetValue (k, lambda));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1031
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1032
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1033
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1034
ErlangRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1035
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1036
  return GetValue (m_k, m_lambda);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1037
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1038
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1039
ErlangRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1040
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1041
  return (uint32_t)GetValue (m_k, m_lambda);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1042
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1043
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1044
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1045
ErlangRandomVariable::GetExponentialValue (double mean, double bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1046
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1047
  while (1)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1048
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1049
      // Get a uniform random variable in [0,1].
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1050
      double v = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1051
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1052
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1053
          v = (1 - v);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1054
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1055
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1056
      // Calculate the exponential random variable.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1057
      double r = -mean*std::log (v);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1058
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1059
      // Use this value if it's acceptable.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1060
      if (bound == 0 || r <= bound)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1061
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1062
          return r;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1063
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1064
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1065
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1066
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1067
NS_OBJECT_ENSURE_REGISTERED(TriangularRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1068
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1069
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1070
TriangularRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1071
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1072
  static TypeId tid = TypeId ("ns3::TriangularRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1073
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1074
    .AddConstructor<TriangularRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1075
    .AddAttribute("Mean", "The mean value for the triangular distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1076
		  DoubleValue(0.5),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1077
		  MakeDoubleAccessor(&TriangularRandomVariable::m_mean),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1078
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1079
    .AddAttribute("Min", "The lower bound on the values returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1080
		  DoubleValue(0.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1081
		  MakeDoubleAccessor(&TriangularRandomVariable::m_min),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1082
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1083
    .AddAttribute("Max", "The upper bound on the values returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1084
		  DoubleValue(1.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1085
		  MakeDoubleAccessor(&TriangularRandomVariable::m_max),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1086
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1087
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1088
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1089
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1090
TriangularRandomVariable::TriangularRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1091
  :
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1092
  m_mode (0.5)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1093
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1094
  // m_mean, m_min, and m_max are initialized after constructor by
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1095
  // attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1096
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1097
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1098
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1099
TriangularRandomVariable::GetMean (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1100
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1101
  return m_mean;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1102
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1103
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1104
TriangularRandomVariable::GetMin (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1105
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1106
  return m_min;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1107
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1108
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1109
TriangularRandomVariable::GetMax (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1110
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1111
  return m_max;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1112
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1113
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1114
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1115
TriangularRandomVariable::GetValue (double mean, double min, double max)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1116
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1117
  // Calculate the mode.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1118
  double mode = 3.0 * mean - min - max;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1119
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1120
  // Get a uniform random variable in [0,1].
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1121
  double u = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1122
  if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1123
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1124
      u = (1 - u);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1125
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1126
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1127
  // Calculate the triangular random variable.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1128
  if (u <= (mode - min) / (max - min) )
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1129
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1130
      return min + sqrt (u * (max - min) * (mode - min) );
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1131
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1132
  else
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1133
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1134
      return max - sqrt ( (1 - u) * (max - min) * (max - mode) );
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1135
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1136
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1137
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1138
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1139
TriangularRandomVariable::GetInteger (uint32_t mean, uint32_t min, uint32_t max)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1140
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1141
  return static_cast<uint32_t> ( GetValue (mean, min, max) );
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1142
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1143
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1144
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1145
TriangularRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1146
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1147
  return GetValue (m_mean, m_min, m_max);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1148
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1149
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1150
TriangularRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1151
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1152
  return (uint32_t)GetValue (m_mean, m_min, m_max);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1153
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1154
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1155
NS_OBJECT_ENSURE_REGISTERED(ZipfRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1156
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1157
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1158
ZipfRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1159
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1160
  static TypeId tid = TypeId ("ns3::ZipfRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1161
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1162
    .AddConstructor<ZipfRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1163
    .AddAttribute("N", "The n value for the Zipf distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1164
		  IntegerValue(1),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1165
		  MakeIntegerAccessor(&ZipfRandomVariable::m_n),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1166
		  MakeIntegerChecker<uint32_t>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1167
    .AddAttribute("Alpha", "The alpha value for the Zipf distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1168
		  DoubleValue(0.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1169
		  MakeDoubleAccessor(&ZipfRandomVariable::m_alpha),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1170
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1171
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1172
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1173
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1174
ZipfRandomVariable::ZipfRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1175
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1176
  // m_n and m_alpha are initialized after constructor by attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1177
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1178
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1179
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1180
ZipfRandomVariable::GetN (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1181
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1182
  return m_n;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1183
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1184
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1185
ZipfRandomVariable::GetAlpha (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1186
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1187
  return m_alpha;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1188
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1189
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1190
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1191
ZipfRandomVariable::GetValue (uint32_t n, double alpha)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1192
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1193
  // Calculate the normalization constant c.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1194
  m_c = 0.0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1195
  for (uint32_t i = 1; i <= n; i++)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1196
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1197
      m_c += (1.0 / std::pow ((double)i,alpha));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1198
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1199
  m_c = 1.0 / m_c;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1200
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1201
  // Get a uniform random variable in [0,1].
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1202
  double u = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1203
  if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1204
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1205
      u = (1 - u);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1206
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1207
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1208
  double sum_prob = 0,zipf_value = 0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1209
  for (uint32_t i = 1; i <= m_n; i++)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1210
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1211
      sum_prob += m_c / std::pow ((double)i,m_alpha);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1212
      if (sum_prob > u)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1213
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1214
          zipf_value = i;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1215
          break;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1216
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1217
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1218
  return zipf_value;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1219
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1220
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1221
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1222
ZipfRandomVariable::GetInteger (uint32_t n, uint32_t alpha)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1223
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1224
  return static_cast<uint32_t> ( GetValue (n, alpha));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1225
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1226
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1227
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1228
ZipfRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1229
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1230
  return GetValue (m_n, m_alpha);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1231
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1232
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1233
ZipfRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1234
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1235
  return (uint32_t)GetValue (m_n, m_alpha);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1236
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1237
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1238
NS_OBJECT_ENSURE_REGISTERED(ZetaRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1239
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1240
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1241
ZetaRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1242
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1243
  static TypeId tid = TypeId ("ns3::ZetaRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1244
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1245
    .AddConstructor<ZetaRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1246
    .AddAttribute("Alpha", "The alpha value for the zeta distribution returned by this RNG stream.",
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1247
		  DoubleValue(3.14),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1248
		  MakeDoubleAccessor(&ZetaRandomVariable::m_alpha),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1249
		  MakeDoubleChecker<double>())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1250
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1251
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1252
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1253
ZetaRandomVariable::ZetaRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1254
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1255
  // m_alpha is initialized after constructor by attributes
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1256
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1257
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1258
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1259
ZetaRandomVariable::GetAlpha (void) const
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1260
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1261
  return m_alpha;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1262
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1263
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1264
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1265
ZetaRandomVariable::GetValue (double alpha)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1266
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1267
  m_b = std::pow (2.0, alpha - 1.0);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1268
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1269
  double u, v;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1270
  double X, T;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1271
  double test;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1272
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1273
  do
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1274
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1275
      // Get a uniform random variable in [0,1].
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1276
      u = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1277
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1278
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1279
          u = (1 - u);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1280
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1281
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1282
      // Get a uniform random variable in [0,1].
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1283
      v = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1284
      if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1285
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1286
          v = (1 - v);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1287
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1288
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1289
      X = std::floor (std::pow (u, -1.0 / (m_alpha - 1.0)));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1290
      T = std::pow (1.0 + 1.0 / X, m_alpha - 1.0);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1291
      test = v * X * (T - 1.0) / (m_b - 1.0);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1292
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1293
  while ( test > (T / m_b) );
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1294
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1295
  return X;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1296
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1297
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1298
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1299
ZetaRandomVariable::GetInteger (uint32_t alpha)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1300
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1301
  return static_cast<uint32_t> ( GetValue (alpha));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1302
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1303
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1304
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1305
ZetaRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1306
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1307
  return GetValue (m_alpha);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1308
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1309
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1310
ZetaRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1311
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1312
  return (uint32_t)GetValue (m_alpha);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1313
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1314
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1315
NS_OBJECT_ENSURE_REGISTERED(DeterministicRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1316
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1317
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1318
DeterministicRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1319
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1320
  static TypeId tid = TypeId ("ns3::DeterministicRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1321
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1322
    .AddConstructor<DeterministicRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1323
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1324
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1325
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1326
DeterministicRandomVariable::DeterministicRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1327
  :
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1328
  m_count (0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1329
  m_next (0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1330
  m_data (0)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1331
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1332
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1333
DeterministicRandomVariable::~DeterministicRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1334
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1335
  // Delete any values currently set.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1336
  if (m_data != 0)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1337
  {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1338
    delete[] m_data;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1339
  }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1340
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1341
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1342
void
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1343
DeterministicRandomVariable::SetValueArray (double* values, uint64_t length)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1344
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1345
  // Delete any values currently set.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1346
  if (m_data != 0)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1347
  {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1348
    delete[] m_data;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1349
  }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1350
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1351
  // Make room for the values being set.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1352
  m_data = new double[length];
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1353
  m_count = length;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1354
  m_next = length;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1355
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1356
  // Copy the values.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1357
  for (uint64_t i = 0; i < m_count; i++)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1358
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1359
      m_data[i] = values[i];
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1360
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1361
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1362
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1363
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1364
DeterministicRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1365
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1366
  // Make sure the array has been set.
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1367
  NS_ASSERT (m_count > 0);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1368
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1369
  if (m_next == m_count)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1370
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1371
      m_next = 0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1372
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1373
  return m_data[m_next++];
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1374
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1375
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1376
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1377
DeterministicRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1378
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1379
  return (uint32_t)GetValue ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1380
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1381
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1382
NS_OBJECT_ENSURE_REGISTERED(EmpiricalRandomVariable);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1383
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1384
// ValueCDF methods
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1385
EmpiricalRandomVariable::ValueCDF::ValueCDF ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1386
  : value (0.0),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1387
    cdf (0.0)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1388
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1389
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1390
EmpiricalRandomVariable::ValueCDF::ValueCDF (double v, double c)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1391
  : value (v),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1392
    cdf (c)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1393
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1394
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1395
EmpiricalRandomVariable::ValueCDF::ValueCDF (const ValueCDF& c)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1396
  : value (c.value),
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1397
    cdf (c.cdf)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1398
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1399
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1400
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1401
TypeId 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1402
EmpiricalRandomVariable::GetTypeId (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1403
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1404
  static TypeId tid = TypeId ("ns3::EmpiricalRandomVariable")
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1405
    .SetParent<RandomVariableStream>()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1406
    .AddConstructor<EmpiricalRandomVariable> ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1407
    ;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1408
  return tid;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1409
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1410
EmpiricalRandomVariable::EmpiricalRandomVariable ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1411
  :
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1412
  validated (false)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1413
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1414
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1415
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1416
double 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1417
EmpiricalRandomVariable::GetValue (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1418
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1419
  // Return a value from the empirical distribution
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1420
  // This code based (loosely) on code by Bruce Mah (Thanks Bruce!)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1421
  if (emp.size () == 0)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1422
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1423
      return 0.0; // HuH? No empirical data
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1424
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1425
  if (!validated)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1426
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1427
      Validate ();      // Insure in non-decreasing
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1428
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1429
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1430
  // Get a uniform random variable in [0,1].
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1431
  double r = Peek ()->RandU01 ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1432
  if (IsAntithetic ())
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1433
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1434
      r = (1 - r);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1435
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1436
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1437
  if (r <= emp.front ().cdf)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1438
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1439
      return emp.front ().value; // Less than first
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1440
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1441
  if (r >= emp.back ().cdf)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1442
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1443
      return emp.back ().value;  // Greater than last
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1444
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1445
  // Binary search
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1446
  std::vector<ValueCDF>::size_type bottom = 0;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1447
  std::vector<ValueCDF>::size_type top = emp.size () - 1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1448
  while (1)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1449
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1450
      std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1451
      if (r >= emp[c].cdf && r < emp[c + 1].cdf)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1452
        { // Found it
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1453
          return Interpolate (emp[c].cdf, emp[c + 1].cdf,
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1454
                              emp[c].value, emp[c + 1].value,
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1455
                              r);
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1456
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1457
      // Not here, adjust bounds
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1458
      if (r < emp[c].cdf)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1459
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1460
          top    = c - 1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1461
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1462
      else
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1463
        {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1464
          bottom = c + 1;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1465
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1466
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1467
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1468
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1469
uint32_t 
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1470
EmpiricalRandomVariable::GetInteger (void)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1471
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1472
  return (uint32_t)GetValue ();
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1473
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1474
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1475
void EmpiricalRandomVariable::CDF (double v, double c)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1476
{ // Add a new empirical datapoint to the empirical cdf
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1477
  // NOTE.   These MUST be inserted in non-decreasing order
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1478
  emp.push_back (ValueCDF (v, c));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1479
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1480
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1481
void EmpiricalRandomVariable::Validate ()
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1482
{
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1483
  ValueCDF prior;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1484
  for (std::vector<ValueCDF>::size_type i = 0; i < emp.size (); ++i)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1485
    {
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1486
      ValueCDF& current = emp[i];
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1487
      if (current.value < prior.value || current.cdf < prior.cdf)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1488
        { // Error
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1489
          std::cerr << "Empirical Dist error,"
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1490
               << " current value " << current.value
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1491
               << " prior value "   << prior.value
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1492
               << " current cdf "   << current.cdf
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1493
                    << " prior cdf "     << prior.cdf << std::endl;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1494
          NS_FATAL_ERROR ("Empirical Dist error");
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1495
        }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1496
      prior = current;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1497
    }
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1498
  validated = true;
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1499
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1500
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1501
double EmpiricalRandomVariable::Interpolate (double c1, double c2,
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1502
                                           double v1, double v2, double r)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1503
{ // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1504
  return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1505
}
32d1fa5dfb9f Port remaining RandomVariable implementations to RandomVariableStream
Mitch Watrous <watrous@u.washington.edu>
parents: 8871
diff changeset
  1506
8871
60846d2741c0 initial patch to fix rng variability
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
diff changeset
  1507
} // namespace ns3