Thread: RNG translation
View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
DiZzY DiZzY is offline
external usenet poster
 
Posts: 1
Default RNG translation

Can someone translate the code below to VBA?

TIA


/*

COMBO

a simple but very good combination generator.
It combines, by addition mod 2^32,
x(n)=x(n-1)*x(n-2) mod 2^32
and
y(n)=30903*y(n-1) + carry mod 2^16
The period of the first is 3*2^29, on odd integers, and the
period of the second, a multiply-with-carry generator, is
30903*2^15-1=1012629503, so the period of combo exceeds 2^60.
This generator is simple to program in Fortran or C and quite
fast.

*/

/*
Simple combo, period 2^60.5
x(n)=x(n-1)*x(n-2) mod 2^32 added to
period of x(n)=x(n-1)*x(n-2) is 3*2^29 if both seeds are
odd, and one is +or-3 mod 8.
easy to ensu replace seed x with 8*seed+3, and y with 2*seed+1
*/


#define ulong unsigned long

static ulong combo_x = 3;
static ulong combo_y = 1;
static ulong combo_z = 1;
static ulong combo_v;

void seed_rand_combo(ulong seed) {
combo_x = seed * 8 + 3;
combo_y = seed * 2 + 1;
combo_z = seed | 1;
}

ulong rand_combo(void) {
combo_v = combo_x * combo_y;
combo_x = combo_y;
combo_y = combo_v;
combo_z = (combo_z & 65535) * 30903 + (combo_z 16);
return combo_y + combo_z;
}