[REBOL] Re: Question and comment about randomizing a block of values
From: joel:neely:fedex at: 18-Jun-2001 18:54
[agem--crosswinds--net] wrote:
> its in my trick-bag now. :) but card-games, ~50?
>
Clearly there's a trade-off between speed and readability.
At ~50 there's not much speed gain to worry about, but
for larger numbers there's an advantage to be had.
OBTW, I did the benchmarking with a slightly (again!)
modified version of my SHUFFLED-IOTA, given below. Using
the square of N for the high-order part limits the maximum
block size to ~1K. Inspired by your larger random range,
I tricked M to use most of the remaining bits for high-
order randomization. Hence:
shuffled-iota1: func [n [integer!] /local m f c r] [
m: to-integer (2 ** 30) / 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
]
A quick bit of benchmarking with the above vs. your version
with SORT/SKIP (generating 1000 blocks with 2000 entries each)
showed times of 5:12 vs. 6:38, about 27% longer for SORT/SKIP.
> performance-hint for great blocks: don't use remove.
> poke deck card-no (last deck) clear back tail deck
>
I guess I'm being dense tonight, but I don't see how that
last line above shuffles the block.
>> tester: iota 10
== [1 2 3 4 5 6 7 8 9 10]
>> poke tester (random 10) (last tester)
== [1 2 3 4 5 6 7 8 9 10]
>> poke tester (random 10) (last tester)
== [1 2 10 4 5 6 7 8 9 10]
>> poke tester (random 10) (last tester)
== [1 10 10 4 5 6 7 8 9 10]
>> poke tester (random 10) (last tester)
== [1 10 10 4 5 6 7 8 9 10]
>> poke tester (random 10) (last tester)
== [1 10 10 4 5 6 10 8 9 10]
It would "overstrike" a random element with the last element
of the block, losing the one POKEd on top of.
OTOH, using ...
>> tester: iota 10
== [1 2 3 4 5 6 7 8 9 10]
>> insert at tester (random 10) last tester
clear back tail tester
== []
>> insert at tester (random 10) last tester
clear back tail tester
== []
>> insert at tester (random 10) last tester
clear back tail tester
== []
>> insert at tester (random 10) last tester
clear back tail tester
== []
>> insert at tester (random 10) last tester
clear back tail tester
== []
>> tester
== [6 1 9 8 10 2 3 4 5 7]
... does get the shuffling done, but now the INSERT is the
culprit for having to scoot the block elements around.
Have I misunderstood something?
> random is random i hope? ;-)
>
Perhaps RANDOM/SECURE is more random than RANDOM ... ;-)
-jn-
------------------------------------------------------------
Programming languages: compact, powerful, simple ...
Pick any two!
joel'dot'neely'at'fedex'dot'com