And while we're on the subject, there is another problem with the new SRFI 27 implementation I've noticed. The randomization of (random-source-pseudo-randomize! s i j) and (random-source-pseudo-randomize! s j i) will always be the same. The code uses equal-hash-code of i and j to come up with a seed and that function is symmetric wrt i and j.
<br><br>I think we still need to do some thinking on the implementation. I think we need to either expose the SRFI 27 interface from PLT Scheme (and implement the old interface on top of that) or separate them completely and go back to the old SRFI 27 implementation. [There would also be some middle ground. But, I don't think there is currently enough of the internal implementation exposed to adequately implement SRFI 27 - there are too many kludges in that code now.]
<br><br>Doug<br><br><div class="gmail_quote">On Dec 30, 2007 10:12 AM, Doug Williams <<a href="mailto:email@example.com">firstname.lastname@example.org</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I sent an answer to Noel directly, but will post an answer for everyone here, too. I believe Noel expected the code sequence '(random-seed 1234) (random-discrete discrete)' to always return the same value - because the random source state should be the same. Unfortunately, it isn't because the science collection uses a different default random source than the PLT Scheme one.
<br><br>The science collection random source and distribution routines are built on top of SRFI 27. At the time the science collection was implemented, the PLT Scheme random source routines were very naive and, therefore, the SRFI 27 implementation was used. Because of restrictions within SRFI 27 (including "Note that an assignment to default-random-source does not change random or random-real; it is also strongly recommended not to assign a new value."), a parameter, current-random-source, was created (in the science collection) to mirror the current-pseudo-random-generator in PLT Scheme.
<br><br>So, back to the '(random-seed 1234) (random-discrete discrete)' sequence. The (random-seed 1234) call sets the seed in the PLT Scheme default random stream (i.e., current-pseudo-random-generator). The (random-discrete discrete) call is using the current-random-source parameter (from the science collection). So, they are using completely different random sources.
<br><br>Another problem is the difference in philosophy between the PLT Scheme random source interface and that of SRFI 27. For 'seeding' a random source, the PLT Scheme philosophy has a single random seed (set by random-seed) [and maps directly to the original internal naive implenentation] and
SRFI uses a 2-dimensional random-source-randomize! described as "Changes the state of the random source s into the initial state of the (i, j)-th independent random source, where i and j are non-negative integers. This procedure provides a mechanism to obtain a large number of independent random sources (usually all derived from the same backbone generator), indexed by two integers. In contrast to random-source-randomize!, this procedure is entirely deterministic."
<br><br>In the 371 -> 372 transition (I believe at 371.1), the PLT Scheme random source routine was changed to use the same algorithm as SRFI 27 and SRFI 27 itself was reimplemented in terms of the new PLT Scheme implementation. There was some breakage to the science collection during that transition, but I think it's all been fixed. [You can look at the change log on PLaneT for details.] However, the random sources are different struct types internally and cannot be used interchangeably. So, we still have the same general problem of two different random source implementations - the science collection happens to use and extend the SRFI 27 one.
<br><br>I'm not sure of the best way to eliminate that confusion in the long term (other than through documentation), but am open to suggestions.<br><font color="#888888"><br>Doug<br><br></font><div class="gmail_quote">
<div class="Ih2E3d">On Dec 29, 2007 9:26 AM, Noel Welsh <
<a href="mailto:email@example.com" target="_blank">firstname.lastname@example.org</a>> wrote:
<br></div><div><div></div><div class="Wj3C7c"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Here's something curious. I have code that samples from a discrete
<br>distribution, and it does not behave as I would expect. The code is
<br>thus:<br><br>(define (sample-cluster-mixture state discrete)<br> (define clusters (state-clusters state))<br> (define i (random-discrete discrete))<br> (printf "SAMPLE CLUSTER MIXTURE ~a\n" i)<br> (printf "~a ~a\n" (discrete-pdf discrete 0)
<br> (discrete-pdf discrete 1))<br> (random-seed 1234)<br> ;(display (random-discrete discrete))<br> (if (zero? i)<br> (sample-new-cluster state)<br> (vector-ref clusters i)))<br><br>The important thing here is the value of i, which is printed out.
<br><br>I call (random-seed 1234) and run it, and the output is this:<br><br>SAMPLE CLUSTER MIXTURE 1<br>0.41176470588235303 0.588235294117647<br>0<br><br>So i is 1<br><br>I uncomment the display line and run it, and get:
<br><br>SAMPLE CLUSTER MIXTURE 0<br>0.41176470588235303 0.588235294117647<br>0<br><br>So now i is 0<br><br>Note that i is evaluated before the call to display. All I can think<br>it that this is an optimiser bug, reordering expressions in some
<br>illegal way. I'm using v371.4 [3m]. I've just svn up'ed and I'll<br>try v4 and 372 to see if there is any difference. If not I'll try to<br>narrow the bug down.<br><br>Consider this a preliminary report only. I was so surprised by this
<br>(having spent an few hours debugging) I had to let the world know...<br><br>N.<br>_________________________________________________<br> For list-related administrative tasks:<br> <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme" target="_blank">