Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++

A Normal / Exponential Random Generator and Histogram Class

Rate me:
Please Sign up or sign in to vote.
4.73/5 (11 votes)
2 Dec 20024 min read 276.3K   8.1K   55  
A fast random generator with normal or exponential distribution + a histogram class
In this article, you will find a fast generator for Random Variable, namely normal and exponential distributions. It is based on George Marsaglia's and Wai Wan Tsang's work.
// HistogramMatlabDlg.cpp : implementation file
//

#include "stdafx.h"
#include "HistogramMatlab.h"
#include "HistogramMatlabDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHistogramMatlabDlg dialog

CHistogramMatlabDlg::CHistogramMatlabDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHistogramMatlabDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHistogramMatlabDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHistogramMatlabDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHistogramMatlabDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHistogramMatlabDlg, CDialog)
	//{{AFX_MSG_MAP(CHistogramMatlabDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHistogramMatlabDlg message handlers

BOOL CHistogramMatlabDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CHistogramMatlabDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CHistogramMatlabDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CHistogramMatlabDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CHistogramMatlabDlg::OnButton1() 
{
	ShowHistogram(ENormal);
}


void CHistogramMatlabDlg::OnButton2() 
{
	ShowHistogram(EExponential);
}


void CHistogramMatlabDlg::ErrHandler(HRESULT hr, EXCEPINFO excep, UINT uArgErr)
{
  if (hr == DISP_E_EXCEPTION)
  {
    TCHAR errDesc[512];
    TCHAR errMsg[512];
    ::_tprintf(errMsg, TEXT("Run-time error %d:\n\n %s"),
            excep.scode & 0x0000FFFF,  //Lower 16-bits of SCODE
            errDesc);                  //Text error description
    ::MessageBox(NULL, errMsg, TEXT("Automation Server Error"),
                MB_SETFOREGROUND | MB_OK);
  }
  else
  {
    LPVOID lpMsgBuf;
    ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                    FORMAT_MESSAGE_FROM_SYSTEM |
                    FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr,
                    MAKELANGID(LANG_NEUTRAL,
                    SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,
                    0, NULL);
    if ((hr == DISP_E_TYPEMISMATCH ) ||
        (hr == DISP_E_PARAMNOTFOUND))
    {
      TCHAR extMess[512];
      ::_tprintf(extMess, TEXT("%s Position of incorrect argument is %u.\n"), (LPCTSTR) lpMsgBuf, uArgErr);
      ::MessageBox(NULL, extMess, TEXT("COM Error"), MB_OK | MB_SETFOREGROUND);
    }
    else
    {
      ::MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("COM Error"),MB_OK | MB_SETFOREGROUND);
    }
    ::LocalFree(lpMsgBuf);
  }
}


void CHistogramMatlabDlg::ShowHistogram(ERandType eType, UINT nSteps)
{
	// Variables that will be used and re-used in our calls
	
	
	DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};
	VARIANT vResult;
	OLECHAR FAR* szFunction;
	HRESULT hr;
	EXCEPINFO excep;
	UINT uArgErr;
	WCHAR szBuffer[512];
		
	// Dispatch ID's for Matlab Application object properties andmethods	
	DISPID dispid_GetFullMatrix;
	DISPID dispid_PutFullMatrix;
	DISPID dispid_Execute;
	DISPID dispid_MinimizeCommandWindow;
	DISPID dispid_GetCharArray;
	DISPID dispid_PutCharArray;
	DISPID dispid_Visible; // 2 possible uses (set/get)
		
	
	IDispatch* pMtlbDispApp=((CHistogramMatlabApp*)AfxGetApp())->GetMatlabEngine();

	// Get the Dispatch Identifiers
	szFunction = OLESTR("GetFullMatrix");
	hr = pMtlbDispApp ->GetIDsOfNames(IID_NULL, &szFunction, 1, LOCALE_USER_DEFAULT, &dispid_GetFullMatrix);
	if (FAILED(hr))
		return;
	
	
	szFunction = OLESTR("PutFullMatrix");
	hr = pMtlbDispApp ->GetIDsOfNames(IID_NULL, &szFunction, 1, LOCALE_USER_DEFAULT, &dispid_PutFullMatrix);
	if (FAILED(hr))
		return;
	
	
	szFunction = OLESTR("Execute");
	hr = pMtlbDispApp ->GetIDsOfNames(IID_NULL, &szFunction, 1, LOCALE_USER_DEFAULT, &dispid_Execute);
	if (FAILED(hr))
		return;

	
	szFunction = OLESTR("MinimizeCommandWindow");
	hr = pMtlbDispApp ->GetIDsOfNames(IID_NULL, &szFunction, 1, LOCALE_USER_DEFAULT, &dispid_MinimizeCommandWindow);
	if (FAILED(hr))
		return;
	
	szFunction = OLESTR("GetCharArray");
	hr = pMtlbDispApp ->GetIDsOfNames(IID_NULL, &szFunction, 1, LOCALE_USER_DEFAULT, &dispid_GetCharArray);
	if (FAILED(hr))
		return;
	
	szFunction = OLESTR("PutCharArray");
	hr = pMtlbDispApp ->GetIDsOfNames(IID_NULL, &szFunction, 1, LOCALE_USER_DEFAULT, &dispid_PutCharArray);
	if (FAILED(hr))
		return;
	
	szFunction = OLESTR("Visible");
	hr = pMtlbDispApp ->GetIDsOfNames(IID_NULL, &szFunction, 1, LOCALE_USER_DEFAULT, &dispid_Visible);
	if (FAILED(hr))
		return;

	
	VARIANT vArgsTypeLong[1];
	::VariantInit(vArgsTypeLong);
	
	vArgsTypeLong[0].vt = VT_I4;
	vArgsTypeLong[0].lVal = 0; // Visible = 1; Invisible = 0
	
	DISPID dispidNamed = DISPID_PROPERTYPUT;
		
	DISPPARAMS dpVisible;
	dpVisible.cArgs = 1;
	dpVisible.cNamedArgs = 1;
	dpVisible.rgvarg = vArgsTypeLong;
	dpVisible.rgdispidNamedArgs = &dispidNamed;
		
	hr = pMtlbDispApp ->Invoke(dispid_Visible, IID_NULL,
		LOCALE_USER_DEFAULT,
		DISPATCH_PROPERTYPUT, &dpVisible, NULL, &excep, &uArgErr);
		
	if (FAILED(hr))
		ErrHandler(hr, excep, uArgErr);

	hr = pMtlbDispApp ->Invoke(dispid_MinimizeCommandWindow,
		IID_NULL,
		LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dpNoArgs, NULL,
		&excep, &uArgErr);
	if (FAILED(hr))
		ErrHandler(hr, excep, uArgErr);
	
	
	// Invoke the PutFullMatrix method:
	// [id(0x60010001)]
	// void PutFullMatrix(
	//                    [in] BSTR Name,
	//                    [in] BSTR Workspace,
	//                    [in] SAFEARRAY(double) pr,
	//                    [in] SAFEARRAY(double) pi);
	
	int n = 50000;
	
	BSTR bstrName;
	bstrName = ::SysAllocString(OLESTR("sample"));
		
	BSTR bstrWorkspace;
	bstrWorkspace = ::SysAllocString(OLESTR("base"));
	
	SAFEARRAYBOUND realPartDims[2];
	realPartDims[0].lLbound = 1;     // Lower bound of the first dimension
	realPartDims[0].cElements = n;
	realPartDims[1].lLbound = 1;     // Lower bound of the second dimension
	realPartDims[1].cElements = 1;
	
	SAFEARRAY *realPart = ::SafeArrayCreate(VT_R8, 2, realPartDims);
		
	long lIndex[2];
	double val;
	int i;
	
	lIndex[1] = 1;

	switch (eType)
	{
	case ENormal:
		for (i = 1; i <= n; i++)
		{
			lIndex[0] = i;		
		
			val = (double)CRandomGenerator::RNOR();
			hr = ::SafeArrayPutElement(realPart, lIndex, &val);
			if (FAILED(hr))
				ErrHandler(hr, excep, uArgErr);
		}
		break;
	case EExponential:
		for (i = 1; i <= n; i++)
		{
			lIndex[0] = i;		
		
			val = (double)CRandomGenerator::REXP();
			hr = ::SafeArrayPutElement(realPart, lIndex, &val);
			if (FAILED(hr))
				ErrHandler(hr, excep, uArgErr);
		}
		break;
	}
		
	VARIANT vArgPutFullMatrix[4];
	for (i = 0; i < 4; ++i)
		::VariantInit(&vArgPutFullMatrix[i]);
	
	
	vArgPutFullMatrix[0].vt = VT_ARRAY | VT_R8;
	vArgPutFullMatrix[0].parray = NULL;			// do set to NULL when	not complex
	vArgPutFullMatrix[1].vt = VT_ARRAY | VT_R8;
	vArgPutFullMatrix[1].parray = realPart;
	vArgPutFullMatrix[2].vt = VT_BSTR;
	vArgPutFullMatrix[2].bstrVal = bstrWorkspace;
	vArgPutFullMatrix[3].vt = VT_BSTR;
	vArgPutFullMatrix[3].bstrVal = bstrName;
	
	
	DISPPARAMS dpPutFullMatrix;
	dpPutFullMatrix.cArgs = 4;
	dpPutFullMatrix.cNamedArgs = 0;
	dpPutFullMatrix.rgvarg = vArgPutFullMatrix;
	
	
	hr = pMtlbDispApp ->Invoke(dispid_PutFullMatrix, IID_NULL,
		LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dpPutFullMatrix, NULL,
		&excep,
		&uArgErr);
	
	
	if (FAILED(hr))
		ErrHandler(hr, excep, uArgErr);
	
	
	::SysFreeString(bstrName);
	::SysFreeString(bstrWorkspace);
	hr = ::SafeArrayDestroy(realPart);

	swprintf( szBuffer, L"hist(sample, %u);", nSteps);
	bstrName = ::SysAllocString(szBuffer);
	
	VARIANT vArgsTypeText[1];
	vArgsTypeText [0].vt = VT_BSTR;
	vArgsTypeText [0].bstrVal = bstrName;
	
	
	DISPPARAMS dpTypeText;
	dpTypeText.cArgs = 1;
	dpTypeText.cNamedArgs = 0;
	dpTypeText.rgvarg = vArgsTypeText;
		
	hr = pMtlbDispApp ->Invoke(dispid_Execute, IID_NULL,
		LOCALE_USER_DEFAULT,
		DISPATCH_METHOD, &dpTypeText, &vResult, &excep, &uArgErr);	
	if (FAILED(hr))
		ErrHandler(hr, excep, uArgErr);
		
	::SysFreeString(bstrName);	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions