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: jeff:rebol at: 19-Jun-2001 9:47

Howdy, Joel:
> >> ? random > USAGE: > RANDOM value /seed /secure /only
> Having seen your example (I like the INSERT/DUP trick, BTW) > I can now be reminded of it by the hints in HELP output, > but I think it would have taken me a VERRRRY long time to > make the leap in the other direction and think of trying > that from the way that RANDOM is described.
RANDOM on a block is a relatively recent addition to REBOL (like core 2.3 and higher I think). RANDOM/only [block] returns a random element from the block. RANDOM [block] returns a randomized version of that block. I think it's a handy shorthand. Also works on other series: rand-alpha: random alpha: "abcdefghijklmnopqrstuvwxyz" ;-- (Creates a copy) The commonly found construct: 'PICK block RANDOM LENGTH? block' can be replaced with 'RANDOM/only block'. And as you've discovered, 'RANDOM block' can replace a considerable variety of code that does the same. :-) This returns us to a unifying concept in REBOL: series as values vs. series as sequential values. RANDOM and RANDOM/only on a block extends the two ways we can think of blocks. We can treat a block as a value itself, or as a sequence of separate values. MOLD/only, RANDOM/only, CHANGE/only, INSERT/only, FIND/only. ONLY allows us to switch our approach to that of "singular" values, we are interested in a single thing. Otherwise we are treating the series as a sequence of things. #1 find [1 2 3 [1 2 3]] [1 2 3] #2 find/only [1 2 3 [1 2 3]] [1 2 3] When I look at the above, in the first one, I mentally strip the outer brackets of the [1 2 3] that we're looking for, and I imagine a full expression that looks like: #1 find [1 2 3 [1 2 3] 1.. 2.. 3.. I want to find the sequence 1 followed by 2 followed by 3. In the second case, I mentally reinforce the surounding brackets of [1 2 3] that's being sought, and I see an expression like: #2 find [1 2 3 [1 2 3]] _[_ 1 2 3 _]_ I want to find THE single block value whose contents are 1 2 3. So the same deal with RANDOM: #1 random [1 2 3] #2 random/only [1 2 3] I mentally convert to: #1 random 1.. 2.. 3.. #2 random _[_ 1 2 3 _]_ #1 I want the random sequence of numbers made from the sequence of numbers 1 followed by 2 followed by 3. #2 I want a single random value from this block of values. Don't mean to lecture (-:, just think this is an important and powerful concept found in series that sometimes gets overlooked. Recognizing this dual nature of REBOL series is very helpful in so many different problem domains. This just seemed an appropriate spot in the thread to rant about the series duality for the benefit of any who may not already have the concept solid in their REBOL mental model. :-) Party on-- -jeff