Jump to content


Click the link below to see the new game I'm developing!


Photo
- - - - -

Random number generator


2 replies to this topic

#1 Wim Soutendijk

Wim Soutendijk

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 216 posts
  • Gender:Male
  • Location:Netherlands

Posted 30 April 2006 - 10:09 AM

In trying to build a random number generator in pro-iv I found the following C code on the web:
#include <stdio.h>
#define A1	7141
#define C1	54773
#define M1	259200
#define RM1	(1.0 / M1)
#define A2	8121
#define C2	28411
#define M2	134456
#define RM2	(1.0 / M2)
#define A3	4561
#define C3	51349
#define M3	243000
#define MASK 131071

float uran3( int *x )
/*---------------------------------------------------------------------
 * This routine generates uniformly distributed random numbers in the
 * range [0, 1) using the linear congruential method.
 * Actually, 3 separate generators are used, generating the upper and
 * lower halves of the output and controlling a shuffling routine.
 *
 * Notes:  
 * 1. A negative seed value initializes the sequence.  The returned
 *    seed is 1.
 * 2. The values A,C, and M are taken from _Numerical Recipes in C_, 
 *    p.211 and are claimed to yield maximal length sequences of period
 *    M and to pass Knuth's spectral test for dimensions 2, 3, 4, 5 & 6.
 *---------------------------------------------------------------------
*/ 
{  static	long	ix1, ix2, ix3;
	static	float	r[98];
	static	int  iff = 0;
	float	temp;
	int  j;

/* init on first call or when *x is negative */
	if ( *x < 0  || iff == 0 ) {
  iff = 1;
  ix1 = (C1 - *x) % M1;
  ix1 = (A1 * ix1 + C1) % M1;
  ix2 = ix1 % M2;
  ix1 = (A1 * ix1 + C1) % M1;
  ix3 = ix1 % M3;
  for ( j = 1;  j < 97;  j++ ) {
 	 ix1 = (A1 * ix1 + C1) % M1;
 	 ix2 = (A2 * ix2 + C2) % M2;
 	 r[j] = (ix1 + ix2 * RM2) * RM1;
  }
  *x = 1;
	}
/* normal operation */
	ix1 = (A1 * ix1 + C1) % M1;
	ix2 = (A2 * ix2 + C2) % M2;
	ix3 = (A3 * ix3 + C3) % M3;
	j = 1 + ((97 * ix3) / M3);
	temp = r[j];
	r[j] = (ix1 + ix2 * RM2) * RM1;
	return( temp );
}

So I created the following global logic RANDOM (type Numeric)

PARMS(#X)
DEFINE #r(98,10.20)
#A1 = 7141
#C1 = 54773
#M1 = 259200
#RM1 = (1 / #M1)
#A2 = 8121
#C2 = 28411
#M2 = 134456
#RM2 = (1 / #M2)
#A3 = 4561
#C3 = 51349
#M3 = 243000
#MASK = 131071
#iff = 0

IF ( #X < 0  OR  #iff = 0 ) THEN
  #iff = 1
  #ix1 = REM((#C1 - #X),#M1 )
  #ix1 = REM((#A1 * #ix1 + #C1),#M1)
  #ix2 = REM(#ix1,#M2)
  #ix1 = REM((#A1 * #ix1 + #C1),#M1)
  #ix3 = REM(#ix1,#M3)
  FOR #j = 1 TO 97
 	 #ix1 = REM((#A1 * #ix1 + #C1),#M1)
 	 #ix2 = REM((#A2 * #ix2 + #C2),#M2)
 	 #r(#j) = (#ix1 + #ix2 * #RM2) * #RM1
  ENDFOR
  #X = 1
ENDIF

#ix1 = REM((#A1 * #ix1 + #C1),#M1)
#ix2 = REM((#A2 * #ix2 + #C2),#M2)
#ix3 = REM((#A3 * #ix3 + #C3),#M3)
#j = 1 + ((97 * #ix3) / #M3)
#temp = #r(#j)
#r(#j) = (#ix1 + #ix2 * #RM2) * #RM1
RETURN(#temp )

But when I call the global, I allways get the same answer back.
My C knowledge is very limited.
Can anyone tell me how the global logic should be created?

#2 Rob Donovan

Rob Donovan

    rob@proivrc.com

  • Admin
  • 1,652 posts
  • Gender:Male
  • Location:Spain

Posted 30 April 2006 - 12:46 PM

Hi,

Its because in the 'C' routine the 'r' array and the 'iif' vars are defined as static.

This means that the routine 'remembers' the values of these vars once the proc has executed, so that when the proc is called for a second time it has them defined with the same values as when it exited the first time.

In ProIV, you would have to make these 2 vars parameters to the GL, and the calling function would have to pass them each time, therefor retaining thier values...

HTHs,

Rob.

#3 andykay

andykay

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 204 posts
  • Gender:Male
  • Location:Cyberspace...looking for work

Posted 01 May 2006 - 05:22 PM

Wim,

There was a previous posting regarding Random Number Generation that also included some code that might be of use to you.

Regards,

AK

http://www.proivrc.c...ead=1&hl=random
THE LIGHT AT THE END OF THE TUNNEL IS THE HEADLAMP OF THE TRAIN THAT'S ABOUT TO HIT YOU!!!



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Click the link below to see the new game I'm developing!