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

[style] Feeling REBOLish (was: Notes on seeding 'random/seed)

 [1/2] from: greggirwin:mindspring at: 21-Mar-2004 11:44


Hi Alan, First, thanks for all your detective work on RANDOM! Wow! I did a few quick hacks against your function (as I added to my storehouse of handy things :), and then I thought maybe it would be useful to share my thoughts with the list. Please don't take these comments as criticism, just thoughts about style and how to make code more "REBOLish". Everyone, please feel free to comment on my thoughts. ap> randomize: func [ ap> {Reseed the random number generator using 'now/precise, or enter a date! time! integer! argument.} ap> seed [any-type!] ap> ][ ap> random/seed either value? 'seed [seed][to-integer checksum/secure form now/precise] ap> ] If you only want to allow date!, time!, and integer!, use that in the spec itself, rather than just as a comment and a spec of any-type!. seed [date! time! integer!] Don't put implementation details ("using 'now/precise") in a function spec comment unless they are important for a user to know about. That is, keep the main comment as concise as possible. {Reseed the random number generator.} If you want to allow any-type! to be used, just feed the seed value you get through the same filter as you do for now/precise. (note that things like function! values still aren't going to work here :) to-integer checksum/secure form seed Rather than using a fixed argument, and checking to see if it has a value, use a refinement that takes a parameter. /with seed I greatly prefer explicitly optional arguments (the refinement approach) for readability and maintenance reasons. ANY can be used as a replacement for EITHER. It's a subjective call, so I often write a piece of code both ways and then pick the one that seems clearer to me. ANY can be faster, but I don't like to give up clarity unless the speed gains are significant (and important). With all that said, here's my version of your function: randomize: func [ "Reseed the random number generator." /with seed ][ random/seed to-integer checksum/secure form any [seed now/precise] ] Thanks again for your post! -- Gregg

 [2/2] from: reboler::programmer::net at: 22-Mar-2004 21:21


Good stuff, Gregg! I like yours better with two exceptions... randomize: func [ "Reseed the random number generator with 'now/precise." /with seed [date! time! integer!] "Enter your own seed." ][ random/seed any [seed to-integer checksum/secure form now/precise] ] It is more REBOLish to have an explicit refinement (just like 'random and 'random/seed), so this is a definite improvement. Replacing 'either with 'any is also an improvement. Part of the whole problem with 'random/seed in the first place is that there was not enough detail given to know the type of arguments accepted. I want to be specific about the usage _and_ the implementation. The user should know _explicitly_ what the seed is when the user asks for help (and not just when the user goes to the source i.e. >>source randomize). This info is often lacking in REBOL functions, especially natives.
>> help randomize
USAGE: RANDOMIZE /with seed DESCRIPTION: Reseed the random number generator with 'now/precise. RANDOMIZE is a function value. REFINEMENTS: /with seed -- Enter your own seed. (Type: date time integer) .... versus ...
>> help random
USAGE: RANDOM value /seed /secure /only DESCRIPTION: Returns a random value of the same datatype. RANDOM is an action value. ARGUMENTS: value -- Maximum value of result (Type: any) REFINEMENTS: /seed -- Restart or randomize ;<<!!! NO INFORMATION GIVEN! /secure -- Returns a cryptographically secure random number. /only -- Return single value from series. The user should also be able to enter their _own_ seed without any processing by the function itself. Thus the user needs to know what data types are accepted. These changes tell the user all he needs to know about using 'randomize. Just my $0.02. Gregg, thanks for your time and interest. Amazing how many ways to skin the cat. Follow-up: Do you think that I should send REBOL feedback on the fact that 'random/seed does not use sub-second precision? IMHO, I think this should be considered a bug in 'random. -- ___________________________________________________________ Sign-up for Ads Free at Mail.com http://promo.mail.com/adsfreejump.htm