473,414 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

random generator

hello.
i need some help with a c or c++ program.
a program for generating random numbers with exponential and poisson
distribution.
please sombody help.
thanks.
Jun 27 '08 #1
3 2969
adonis wrote:
hello.
i need some help with a c or c++ program.
a program for generating random numbers with exponential and poisson
distribution.
please sombody help.
thanks.
Dear Adonis,

This is not a C++ problem. In standard C++ and C you have a uniform
random number generator (rand(), returns an int between 0 and RAND_MAX).
you can rescale the range (e.g., a doublel from 0 to 1.0) and than use
some mathematics to produce another distribution that you like.

Best wishes,

Giuseppe
Jun 27 '08 #2
Boost has a random number library

http://www.boost.org/doc/libs/release/libs/random

Note that they are deterministically random generators
Jun 27 '08 #3
adonis wrote:
hello.
i need some help with a c or c++ program.
a program for generating random numbers with exponential
Take a variable X uniformly distributed in [0,1) and return

- a * log( 1.0 - X )
and poisson distribution.
That is much more tricky. The following is an implementation of the
poission_distribution from C++0X. You will not be able to use the code as
it is, but it should give you an idea as to how one can go about this.
// poisson_distribution.cc, Kai-Uwe Bux [2007-2008]
// ================================================

// Copyright notice
// ================
/*
This code is placed into the public domain by the author.
*/

// DISCLAIMER
// ==========
/*
This code may have undefined behavior and/or
fry your computer. Use at your own risk. You
have been warned.
*/

// Description
// ===========
/*
This implements poisson_distribution<from std.
*/

// Method
// ======
/*
J.H. Ahrens, U. Dieter: "Computer Generation of Poisson
Deviates from Modified Normal Distributions", ACM Transactions
on Mathematical Software 8 (1982) 163-179

BEWARE: there is a typo in Procedure F in the paper, it is
corrected below.
*/
#ifndef KUBUX_poisson_distribution
#define KUBUX_poisson_distribution

#include "contract.cc"
#include "constants.cc"
#include "polynomial.cc"
#include "uniform_real_distribution.cc"
#include "exponential_distribution.cc"
#include "normal_distribution.cc"
#include <cmath>
#include <math.h>
#include <vector>
#include <algorithm>
#include <limits>

namespace kubux {

using std::vector;
using std::upper_bound;
using std::abs;
using std::exp;
using std::log;
using std::pow;
using std::sqrt;
using std::numeric_limits;
template < typename IntType = int >
class poisson_distribution {
private:

typedef double float_type;

typedef uniform_real_distribution< float_type uniform_dist;
typedef exponential_distribution< float_type exponential_dist;
typedef normal_distribution< float_type normal_dist;

typedef typename uniform_dist::param_type uniform_param;
typedef typename normal_dist::param_type normal_param;

public:

typedef IntType result_type;

class param_type {

friend class poisson_distribution;

fhuge m;

// Case A:
fhuge s;
fhuge d;
fhuge L;

fhuge omega;
fhuge b1;
fhuge b2;
fhuge c3;
fhuge c2;
fhuge c1;
fhuge c0;
fhuge c;

mutable fhuge px;
mutable fhuge py;
mutable fhuge fx;
mutable fhuge fy;

void procedure_F ( result_type k ) const {
static fhuge a [10] = {
-0.5000000002L,
0.3333333343L,
-0.2499998565L,
0.1999997049L,
-0.1666848753L,
0.1428833286L,
-0.1241963125L,
0.1101687109L,
-0.1142650302L,
0.1055093006L };
step_1 :
if ( k < 10 ) {
px = -m;
py = pow( m, fhuge(k) ) / tgamma(fhuge(k+1));
} else {
fhuge delta = ( 1.0L / 12.0L ) / fhuge(k);
delta -= 4.8L * delta * delta * delta;
fhuge v = ( m - fhuge(k) ) / fhuge(k);
px = fhuge(k) * log( 1.0L + v ) - ( m - fhuge(k) ) - delta;
if ( -0.25L <= v && v <= 0.25L ) {
fhuge poly = eval_polynomial( a, a+10, v );
px = fhuge(k) * v*v * poly - delta;
} else {
px = fhuge(k) * log( 1.0L + v ) - ( m - fhuge(k) ) - delta;
}
py = ( 1.0L / sqrt2pi ) / sqrt(fhuge(k));
}
step_2 :
fhuge x = ( fhuge(k) - m + 0.5L ) / s;
x *= x;
// NOTE: [typo in the paper]
/*
The paper states the algorithm with
f_x <-- 0.5 X^2
However, this contradicts the (correct) formula (13).
The sign got lost in translation. We put it back in.
*/
fx = -0.5L * x;
fy = omega *
( ( ( c3 * x + c2 ) * x + c1 ) * x + c0 );
}

// Case B:
mutable fhuge p_last;
mutable vector< fhuge cumm;

result_type grow_table ( float_type uniform_variate ) const {
result_type k =
upper_bound( cumm.begin(), cumm.end(), uniform_variate )
-
cumm.begin();
if ( k == cumm.size() ) {
while ( cumm.size() <= 35 && cumm.back() < uniform_variate ) {
p_last *= m/float_type( cumm.size() );
cumm.push_back ( cumm.back() + p_last );
}
return ( cumm.size() - 1 );
}
return ( k );
}
template < typename UniformRNG >
result_type draw ( UniformRNG & urng,
uniform_dist & uni_d,
normal_dist & nor_d,
exponential_dist & exp_d ) const {
fhuge u;
fhuge g;
result_type k;
fhuge e;
fhuge t;
fhuge dummy;
if ( this->m < 10.0 ) {
return( this->grow_table( uni_d( urng ) ) );
} else {
step_N :
g = nor_d( urng, normal_param( this->m, this->s ) );
k = floor(g);
if ( g < 0.0 ) {
goto step_P;
}
step_I :
if ( fhuge(k) >= this->L ) {
return ( k );
}
step_S:
u = uni_d( urng );
dummy = this->m - fhuge(k);
if ( this->d * u >= dummy * dummy * dummy ) {
return ( k );
}
step_P :
if ( g >= 0.0 ) {
this->procedure_F( k );
} else {
goto step_E;
}
step_Q :
if ( this->fy * ( 1.0L - u )
<=
this->py * exp( this->px - this->fx ) ) {
return ( k );
}
step_E :
e = exp_d( urng );
u = uni_d( urng, uniform_param( -1.0L, 1.0L ) );
t = 1.8L + ( u 0 ? e : -e );
if ( t <= -0.6744L ) {
goto step_E;
}
k = floor( this->m + this->s * t );
this->procedure_F( k );
step_H :
if ( this->c * abs(u)
>
this->py * exp( this->px + e )
-
this->fy * exp( this->fx + e ) ) {
goto step_E;
}
return ( k );
}
}
public:

typedef poisson_distribution distribution_type;

param_type ( float_type m_par )
: m ( m_par )

, s ( sqrt( m ) )
, d ( 6.0L * m*m )
, L ( floor( m - 1.1484L ) )

, omega ( ( 1.0L / sqrt2pi ) / s )
, b1 ( ( 1.0L / 24.0L ) / m )
, b2 ( 0.3L * b1*b1 )
, c3 ( ( 1.0L / 7.0L ) * b1 * b2 )
, c2 ( b2 - 15.0L * c3 )
, c1 ( b1 - 6.0L * b2 + 45.0L * c3 )
, c0 ( 1.0L - b1 + 3.0L * b2 - 15.0L * c3 )
, c ( 0.1069L / m )

, p_last ( exp( -m ) )
, cumm ()
{
cumm.push_back( p_last );
KUBUX_ENFORCE( float_type(0) < m );
}

result_type mean ( void ) const {
return ( m );
}

};

private:

param_type the_parm;
uniform_dist uni_d;
normal_dist nor_d;
exponential_dist exp_d;

public:

explicit
poisson_distribution ( float_type mean = 1.0 )
: the_parm( mean )
, uni_d ( 0, 1 )
, nor_d ( 0, 1 )
, exp_d ( 1 )
{}

explicit
poisson_distribution ( param_type const & parm )
: the_parm ( parm )
, uni_d ( 0, 1 )
, nor_d ( 0, 1 )
, exp_d ( 1 )
{}

param_type param ( void ) const {
return ( the_parm );
}

void param ( param_type const & parm ) {
the_parm = parm;
}

float_type mean ( void ) const {
return ( the_parm.mean() );
}

result_type min ( void ) const {
return ( 0 );
}

result_type max ( void ) const {
return ( numeric_limits<result_type>::max() );
}

template < typename UniformRNG >
result_type operator() ( UniformRNG & urng,
param_type const & parm ) {
return( parm.draw( urng, uni_d, nor_d, exp_d ) );
}

template < typename UniformRNG >
result_type operator() ( UniformRNG & urng ) {
return ( this->operator()( urng, the_parm ) );
}

void reset ( void ) {
uni_d.reset();
nor_d.reset();
exp_d.reset();
}

};

} // namespace kubux

#endif // KUBUX_poisson_distribution

// end of file

Best

Kai-Uwe Bux
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
2
by: Matthew Wilson | last post by:
The random.jumpahead documentation says this: Changed in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n) jumps to another state likely to be separated by many...
3
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.