[REBOL] Re: Question and comment about randomizing a block of values
From: joel:neely:fedex at: 18-Jun-2001 15:48
Hi, again, Volker!
> [joel--neely--fedex--com] wrote:
> > shuffled-iota: func [n [integer!] /local m f c r] [
> > m: n * n
> > c: -1
> > r: make block! n
> > loop n [append r (n * random m) + c: c + 1]
> > sort r
> > forall r [change r r/1 // n + 1]
> > head r
> > ]
> ;Aargh! whats this? you are going to beat Ladislav in
Who should be more offended, Ladislav or me? ;-)
> ;specially tricky the hidden assignment of 'c ! :)
It's not hidden; it's right there in plain sight!
Actually, one of the aspects of Jeff's post on "REBOL Zen"
was the fact that many REBOL expressions have both a result
value and an effect (not to say "side-"effect), and that
tense, well-bummed code makes use of both.
> ; after puzzling a while i think you do this? :
You certainly got the essence of SHUFFLED-IOTA , but ...
> ???: func ["set-word!-?? , dump-tool" 'word value] [
> print [mold :word " : " mold :value] word :value]
> shuffled-iota: func [n [integer!] /local f r r1 i] [
> f: 2 ** 30
> ;make a block of pairs [57 1 82 2 16 3 ..]
> r1: make block! n * 2
> repeat i n [append r1 reduce [random f i]] ;flat block!
> sort/skip ?? r1 2 ;shuffle-map ready
> ? r1
> ;now keep only the indices, kill randoms
> r: make block! n
> forskip r1 2 [append r second r1]
... I used arithmetic instead of a pair-block because I
wanted to squeeze all of the performance I could out of
SHUFFLED-IOTA. You've inspired me to run some benchmarks
on how much difference it makes, however.
> ??? test-data: repeat i 20 [append  i] ;never in function!
And *you* accused *me* of being tricky??? I'm going to have
to remember *that* tasty little trick, I assure you!
Programming languages: compact, powerful, simple ...
Pick any two!