I’m running into something very strange in the context of a larger script that means that I either misunderstand what Pxrand does, or there’s a bug or something …?
I’m testing the most basic Pxrand script and outputting to the post window:
var g = Pxrand.new([1,2,3,4,5,6,7,8,9,10], inf);
g.asStream.next
When I run this I can see that numbers from the list are repeated multiple times in a row, quite frequently. My understanding is that Pxrand is meant to avoid this exact behaviour so that that same item is never repeated consecutively.
There is no bug here. You can think of a pattern as a blueprint for a machine, and a stream as the actual constructed machine. Every time you run g.asStream, you are creating a new stream from that blueprint, independent of any other stream created from the same pattern object. For example, consider the same code using Pseq instead of Pxrand:
g = Pseq.new([0, 1, 2], inf)
g.asStream.next
If you evaluate this multiple times, you’ll only ever see 0, because you are just asking for the first value of a new stream each time.
What you want instead is:
h = g.asStream;
h.next; // this you can evaluate repeatedly to get the results you want
you can also use g.asStream.nextN(n) to get the first n values from the stream.
you are not posting the whole code but I assume generating new Streams from a Pxrand Pattern in each loop is causing the repetitions. This is touching the crucial difference between Patterns and Streams (see James Harkins’ Pattern Guide for this and other Pattern subtleties)
Yep, that’s precisely the issue I was running into… I don’t think I fully grokked the relationship between the Pattern and the Stream, and was thinking of the stream as a direct reference to the pattern, but just returning values in a different way. I didn’t full get that each stream is independent and only references the the pattern as a source.
I think I understand it better now! Thanks for your quick help.