RandomSeed Protection.
- To: mathgroup at smc.vnet.net
- Subject: [mg71907] RandomSeed Protection.
- From: "Philipp" <Philipp.M.O at gmail.com>
- Date: Sun, 3 Dec 2006 06:26:26 -0500 (EST)
Did you ever get caught putting a function in your Mathematica code
that doesn't exist, then debugging it "until kingdom came", never using
Trace on the offending statement?
I do it sometimes, and it usually ruins my day. Especially, with
innocuous looking symbols such as RandomSeed (which is actually defined
in the System context) or FileName (also defined in System`) instead of
SeedRandom and ToFileName, etc.
Here is a code snippet that will throw Message in both situations
(* ======================== *)
Begin["System`"]
General::"NoSym" = "The `1` symbol does NOT exist.`2`"
$NoSymbolQ = True;
Unprotect[RandomSeed];
RandomSeed[args___] := \
Block[{$NoSymbolQ=False}, \
Message[General::"NoSym", "RandomSeed", \
" Did you mean \"SeedRandom\"?"]; \
RandomSeed[args]] /; $NoSymbolQ
Protect[RandomSeed];
FileName[args___] := \
Block[{$NoSymbolQ=False}, \
Message[General::"NoSym", "FileName", \
" Did you mean \"ToFileName\"?"]; \
FileName[args]] /; $NoSymbolQ
End[(* System` *)]
(* ======================== *)
Put it at the end of the Kernel init.m file in
(...\Wolfram Research\Mathematica\5.2\Configuration\Kernel) directory,
and extent as needed.
Now, the offenders will generate messages;
In[1]:= RandomSeed[12];
General::NoSym: The RandomSeed symbol does NOT exist. Did you
mean "SeedRandom"?
In[2]:= FileName[{"abc", "dir"}, "file.txt"];
General::NoSym: The FileName symbol does NOT exist. Did you
mean "ToFileName"?
Setting $NoSymbolQ to False will turn off the mechanism.
Cheers,
Philipp.