473,324 Members | 2,456 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,324 software developers and data experts.

How to modify the CMWC4096 PRNG to make an LCG of it?

I put the CMWC4096 PRNG of Prof. Marsaglia into a
simple C++ class. It works as intended, ie. as a 32-bit PRNG.

But my question is:
what must be changed in the code to make an LCG of it?
< fup set to sci.math.num-analysis >
file: Rand_CMWC4096.hpp
#ifndef Rand_CMWC4096_hpp
#define Rand_CMWC4096_hpp

/************************************************** *****************
CMWC4096 ported to C++
For the original C code as was posted by Prof. George Marsaglia to sci.math.num-analysis see:
http://groups.google.com/group/sci.m...37cb18f98210e8
It is also discussed in his paper
Random Number Generators, Journal of Modern Applied Statistical Methods, May 2003, Vol 2 No 1; 2-13

The following is just my Q&D port to C++ :
*/
#ifdef _MSC_VER
#define UINT32 unsigned long
#define UINT64 unsigned __int64
#else
/* we assume it's a std unix/linux, nothing exotic */
#define UINT32 unsigned long
#define UINT64 unsigned long long
#endif

#include <stdlib.h>
class Rand_CMWC4096
{ // 32-bit Rand, but internally uses also two 64-bit fields (a and t)

private:
#define NSEED 4096U /* don't change; or consult the paper before changing */
UINT32 Q[NSEED], c, i;
const UINT64 a;
const UINT32 r;

public:
Rand_CMWC4096() : c(362436), a(18782), r(0xfffffffe), i(NSEED - 1)
{ // initialize the RNG

// init seedtab with random values; here using the systems' ::srand() and ::rand() !!!
const UINT32 myInitialSeed = 4711;
::srand(myInitialSeed); // system RNG
UINT32 j;
for (j = 0; j < NSEED; ++j)
Q[j] = ::rand(); // system RNG

// generate rands by using and overwriting the seed tab:
srand(myInitialSeed); // this RNG
for (j = 0; j < NSEED; ++j)
rand();
}

void srand(UINT32 s)
{
c = s % a;
}

UINT32 rand(UINT64 An = 0x100000000UL)
{
i = (i + 1U) & (NSEED - 1U);
UINT64 t = UINT32(a * Q[i] + c);
c = UINT32(t >32U);
UINT32 x = UINT32(t + c);

if (x < c)
{
++x;
++c;
}

if ((x + 1) == 0)
{
x = 0;
++c;
}

Q[i] = r - x;

return UINT32(UINT64(Q[i]) % An);
}
};

#endif

Jun 27 '08 #1
0 1158

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

Similar topics

0
by: Jean-Michel Besnard | last post by:
Hi, with gcc under GNU/Linux I can make use of setstate() and initstate() to work out the state of my pseudo random number generator but I am unable to find same or similar functions to use with...
7
by: Protoman | last post by:
I need to write a cryptographically secure pseudorandom number generator for a crypto app I'm writing; if you could give me some example code, that'd be great. Thanks for the help!!!
12
by: SStory | last post by:
Doing pages for contract..... If I make an ASPX file that does certain things, how simple would it be for a person who know nothing about it to modify the user interface without bothering the...
31
by: pinkfloydhomer | last post by:
Using rand() in and old version og gcc, and using Tausworth's method, I calculated the frequency of 0 or 1 in the first digit like this: int hist = {0,0}; for (i = 0; i < 100000; ++i) {...
23
by: no1zson | last post by:
I have been adding buttons to my GUI to manipulate list data. I added a Delete button this morning in case I decide I no longer needed a particular element. I am now working on a modify button, in...
4
by: Alan Silver | last post by:
Hello, Is it possible to modify the templates that come with VS2005? I have searched all over the place, but can't find them anywhere. I know I can make my own, but I would like to know if I can...
55
by: copx | last post by:
Can anyone point me to a simple, fast RRNG function to generate random ints within a specified range? It is important that each value within the range has the same probability (uniform...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.