Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Question and comment about randomizing a block of values

From: joel:neely:fedex at: 19-Jun-2001 12:07

Hey, Scott, Which part? See below GS Jones wrote:
> From: "Jeff Kreis" > > random/seed now/time > > c: 0 > > x: random reduce head insert/dup copy [] [c: c + 1] 52 > > > > ; -- produces a block of 1 - 52 randomly ordered > > I've been thinking about this on and off for two hours, and I > still do not understand how this statement works. Can someone > state what is going on in other words? >
The deep magic (the part I didn't know about ;-) is that RANDOM will take a block and return a "randomized" copy of the block:
>> a: [1 2 3 4] == [1 2 3 4] >> random a == [3 2 4 1] >> random a == [3 2 1 4] >> random a == [4 3 2 1] >> random a == [3 1 2 4]
in contrast to RANDOM/ONLY which returns a random selection from the block
>> random/only a == 3 >> random/only a == 1 >> random/only a == 4 >> random/only a == 3
The cool trick on the right-hand side is to use an anonymous block that is extended with instructions to count up, and then is reduced. Note the addition of PROBEs... c: 0 x: probe random probe reduce probe head insert/dup copy [] [c: c + 1] 8 which yields [ c: c + 1 c: c + 1 c: c + 1 c: c + 1 c: c + 1 c: c + 1 c: c + 1 c: c + 1] A block containing 8 copies of C: C + 1 will reduce to [1 2 3 4 5 6 7 8] (if C is initialized to zero, of course). This randomizes to [7 6 4 3 5 2 1 8] Cool, huh? Thanks again, Jeff! -jn- ___ ___ ___ \/ 2 + \/ 2 = 4 (for sufficiently large approximations of \/ 2 ) joel'dot'neely'at'fedex'dot'com