object as parameter vs object method

Thread Solved
Reply

Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

object as parameter vs object method

 
0
  #1
Aug 20th, 2008
Hi I'm doing a statistical mcmc model where I need some random numbers.
I decided to use the ranrot-w generator.

The random number only works when I call the randomnumber method directly,
and not when i give the random number as an parameter to another function.
that is

rg.Random() !=random(rg)

where random simply calls the Random method in the class.

Below is a cut-down code snippet, that illustrates the problem,
it compiles and works under the g++ compiler.

and also the output from the program, it clearly show the the randomizer returns the same value, when called indirectly.

thanks in advance


  1. dddddddddddddddddddddddddddddddd: 0.571800
  2. dddddddddddddddddddddddddddddddd: 0.493439
  3. dddddddddddddddddddddddddddddddd: 0.518434
  4. dddddddddddddddddddddddddddddddd: 0.322902
  5. dddddddddddddddddddddddddddddddd: 0.996980
  6. dddddddddddddddddddddddddddddddd: 0.188140
  7. dddddddddddddddddddddddddddddddd: 0.188140
  8. dddddddddddddddddddddddddddddddd: 0.188140
  9. dddddddddddddddddddddddddddddddd: 0.188140
  10. dddddddddddddddddddddddddddddddd: 0.188140

  1.  
  2. #include <iostream>
  3.  
  4. #include <math.h>
  5.  
  6. #include <stdio.h>
  7.  
  8. // Define 32 bit signed and unsigned integers.
  9. // Change these definitions, if necessary, on 64 bit computers
  10. typedef signed long int32;
  11. typedef unsigned long uint32;
  12.  
  13. class TRanrotWGenerator { // encapsulate random number generator
  14. enum constants { // define parameters
  15. KK = 17, JJ = 10, R1 = 19, R2 = 27};
  16. public:
  17. double random_normal(double esp,double var);
  18. void RandomInit(uint32 seed); // initialization
  19. int IRandom(int min, int max); // get integer random number in desired interval
  20. long double Random(); // get floating point random number
  21. uint32 BRandom(); // output random bits
  22. TRanrotWGenerator(uint32 seed); // constructor
  23. protected:
  24. int p1, p2; // indexes into buffer
  25. union { // used for conversion to float
  26. long double randp1;
  27. uint32 randbits[3];};
  28. uint32 randbuffer[KK][2]; // history buffer
  29. uint32 randbufcopy[KK*2][2]; // used for self-test
  30. enum TArch {LITTLEENDIAN, BIGENDIAN, NONIEEE, EXTENDEDPRECISIONLITTLEENDIAN};
  31. TArch Architecture; // conversion to float depends on computer architecture
  32. };
  33.  
  34.  
  35. //these functions are randomizer relateded and used by main bamse
  36.  
  37. float FRandom(float themin,float themax,TRanrotWGenerator rg);
  38.  
  39.  
  40. uint32 _lrotl (uint32 x, int r) {
  41. return (x << r) | (x >> (sizeof(x)*8-r));}
  42.  
  43.  
  44. // constructor:
  45. TRanrotWGenerator::TRanrotWGenerator(uint32 seed) {
  46. RandomInit(seed);
  47. // detect computer architecture
  48. randbits[2] = 0; randp1 = 1.0;
  49. if (randbits[2] == 0x3FFF) Architecture = EXTENDEDPRECISIONLITTLEENDIAN;
  50. else if (randbits[1] == 0x3FF00000) Architecture = LITTLEENDIAN;
  51. else if (randbits[0] == 0x3FF00000) Architecture = BIGENDIAN;
  52. else Architecture = NONIEEE;}
  53.  
  54. uint32 TRanrotWGenerator::BRandom() {
  55. // generate next random number
  56. uint32 y, z;
  57. // generate next number
  58. z = _lrotl(randbuffer[p1][0], R1) + randbuffer[p2][0];
  59. y = _lrotl(randbuffer[p1][1], R2) + randbuffer[p2][1];
  60. randbuffer[p1][0] = y; randbuffer[p1][1] = z;
  61. // rotate list pointers
  62. if (--p1 < 0) p1 = KK - 1;
  63. if (--p2 < 0) p2 = KK - 1;
  64. // perform self-test
  65. if (randbuffer[p1][0] == randbufcopy[0][0] &&
  66. memcmp(randbuffer, randbufcopy[KK-p1], 2*KK*sizeof(uint32)) == 0) {
  67. // self-test failed
  68. if ((p2 + KK - p1) % KK != JJ) {
  69. // note: the way of printing error messages depends on system
  70. // In Windows you may use FatalAppExit
  71. printf("Random number generator not initialized");}
  72. else {
  73. printf("Random number generator returned to initial state");}
  74. exit(1);}
  75. randbits[0] = y;
  76. randbits[1] = z;
  77. return y;
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84. int TRanrotWGenerator::IRandom(int min, int max) {
  85. // get integer random number in desired interval
  86. int retVal;
  87. int iinterval = max - min + 1;
  88. if (iinterval <= 0) return 0x80000000; // error
  89. int i = int(iinterval * Random()); // truncate
  90. if (i >= iinterval) i = iinterval-1;
  91. retVal = min + i;
  92. //printf("iiiiiiiiiiiiiiiiiiii: \t %d\n",retVal);
  93. return retVal;}
  94.  
  95.  
  96. void TRanrotWGenerator::RandomInit (uint32 seed) {
  97. // this function initializes the random number generator.
  98. int i, j;
  99.  
  100. // make random numbers and put them into the buffer
  101. for (i=0; i<KK; i++) {
  102. for (j=0; j<2; j++) {
  103. seed = seed * 2891336453UL + 1;
  104. randbuffer[i][j] = seed;}}
  105. // set exponent of randp1
  106. randbits[2] = 0; randp1 = 1.0;
  107. // initialize pointers to circular buffer
  108. p1 = 0; p2 = JJ;
  109. // store state for self-test
  110. memcpy (randbufcopy, randbuffer, 2*KK*sizeof(uint32));
  111. memcpy (randbufcopy[KK], randbuffer, 2*KK*sizeof(uint32));
  112. // randomize some more
  113. for (i=0; i<31; i++) BRandom();
  114. }
  115.  
  116.  
  117. long double TRanrotWGenerator::Random() {
  118. // returns a random number between 0 and 1.
  119. uint32 z = BRandom(); // generate 64 random bits
  120.  
  121. switch (Architecture) {
  122. case EXTENDEDPRECISIONLITTLEENDIAN:
  123. // 80 bits floats = 63 bits resolution
  124. randbits[1] = z | 0x80000000; break;
  125. case LITTLEENDIAN:
  126. // 64 bits floats = 52 bits resolution
  127. randbits[1] = (z & 0x000FFFFF) | 0x3FF00000; break;
  128. case BIGENDIAN:
  129. // 64 bits floats = 52 bits resolution
  130. randbits[0] = (randbits[0] & 0x000FFFFF) | 0x3FF00000; break;
  131. case NONIEEE: default:{
  132. double retVal = (double)z * (1./((double)(uint32)(-1L)+1.));
  133. printf("ddddddddddddddddddddddddddddddddddd:\t\t%f\n",retVal);
  134. // not a recognized floating point format. 32 bits resolution
  135. return retVal;}
  136. }
  137. return randp1 - 1.0;}
  138.  
  139.  
  140.  
  141.  
  142. float FRandom(float themin,float themax,TRanrotWGenerator rg){
  143. //printf("->themin%f\tthemax%f\n",themin,themax);
  144. float vars = (themax-themin);
  145. float vars1 = rg.Random();
  146. //printf("->vars:%f\t\tvars1:%f\n",vars,vars1);
  147. return ((themax-themin)*vars1+themin);
  148. }
  149.  
  150. using namespace std;
  151.  
  152. int main(){
  153. TRanrotWGenerator rg = TRanrotWGenerator(0);
  154. for (int i=0;i<5;i++)
  155. int var= rg.Random ();
  156. for (int i=0;i<5;i++)
  157. int var1= FRandom (0, 1,rg);
  158. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: object as parameter vs object method

 
0
  #2
Aug 20th, 2008
Hi again,
I realized that objects is passed by value, so I just need to and and & to the function declaration.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC