In Mark Steyvers & Tom Griffiths's Matlab Topic Modeling Toolbox 1.4 http://psiexp.ss.uci.edu/research/programs_data/toolbox.htm, in the file "GibbsSamplerLDA.cpp", line48-56:

  for (i=0; i<(n-1); i++) {
  // pick a random integer between i and nw
  rp = i + (int) ((double) (n-i) * (double) randomMT() / (double) (4294967296.0 + 1.0));

  // switch contents on position i and position rp
  temp = order[rp];
  order[rp]=order[i];
  order[i]=temp;

}

can any one tell me what's this used for? and why " rp = i + (int) ((double) (n-i) * (double) randomMT() / (double) (4294967296.0 + 1.0));" this is reasonable? many thanks!!

asked Apr 08 '11 at 03:49

ylqfp's gravatar image

ylqfp
0334

edited Apr 08 '11 at 03:51


One Answer:

Firstly, this is not a Matlab / Math help forum, it's for machine learning questions. Your question might get closed or moved somewhere. With that said ...

randomMT() is a C/C++ function that returns an unsigned 32-bit int. 4294967296 is 2^32, so adding 1 to it would at least mathematically force that ratio, where the numerator is a 32-bit unsigned int, into the range [0,1) (note that 1.0 is explicitly excluded). Assuming the machine precision is good enough, then that ratio will never result in a value of 1.0 (up to 15 digits of precision).

To break this down more succinctly:

  • (double) randomMT() // Casts the uint32 to a double
  • (double) randomMT() / (double)( 4294967296.0 + 1.0) // Result is a random double in the range [0,1)
  • (double)(n-i) * (...) // Scales it so you end up with a random double in the range [0,n-i)
  • i + (...) // Shifts it so you have a random double in the range [i,n)

The only reason I can think for them to use 4294967296.0 + 1.0 is to make it easier to read if you recognize that 2^32 = 4294967296.

Lastly, I'm not sure I see a need to add the 1.0 in the first place. The max value for a uint32 is 2^32 - 1, so randomMT() / (double)(2^32) should've been sufficient.

Anyway, I'm done.

-Brian

This answer is marked "community wiki".

answered Apr 08 '11 at 16:39

Brian%20Vandenberg's gravatar image

Brian Vandenberg
824213746

edited Apr 08 '11 at 16:43

Sorry for all the edits. The preview window doesn't render nested lists the same as the final version.

(Apr 08 '11 at 16:43) Brian Vandenberg

sorry ,i don't know where to ask this problem, just know metaop... Maybe my expression is not very correct, i mainly intended to ask what's the code "// switch contents on position i and position rp temp = order[rp]; order[rp]=order[i]; order[i]=temp;" for , many thanks again!

(Apr 08 '11 at 22:16) ylqfp
Your answer
toggle preview

powered by OSQA

User submitted content is under Creative Commons: Attribution - Share Alike; Other things copyright (C) 2010, MetaOptimize LLC.