Re: Random Trouble
- To: mathgroup at smc.vnet.net
- Subject: [mg39224] Re: [mg39178] Random Trouble
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 4 Feb 2003 02:22:38 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On Monday, February 3, 2003, at 09:42 AM, Donald A. Darling wrote: > > > > On Sunday, February 2, 2003, at 03:13 PM, Donald Darling wrote: > >> I have a program which uses a random number in several places - the >> same >> number in a given run of the program. When I implement the program >> however >> the number changes in every new call to it. I've tried to overcome >> this >> using "Which", "Hold", "Verbatim", and others, all to no avail. Any >> help >> greatly appreciated. >> >> >> >> >> > > This is what SeedRandom is for, e.g. > > > SeedRandom[5];Table[Random[Integer,{1,10}],{3}] > > > {2,3,2} > > Now evaluating again: > > > SeedRandom[5];Table[Random[Integer,{1,10}],{3}] > > > {2,3,2} > > > Andrzej Kozlowski > Yokohama, Japan > http://www.mimuw.edu.pl/~akoz/ > http://platon.c.u-tokyo.ac.jp/andrzej/ > > Andrzej - Thanks for the tip on SeedRandom, which a few respondents > suggested. There is some difficulty. If I try > > F[n_]= SeedRandom;Table[Random[Real,{-1,1}],{n}] > > with n=5, say, I get an error message. I don't know how to fix this. > > Don Darling. > > > > You need to use a "seed" with SeedRandom and you need to enclose the composite statement in parentheses: F[n_] := (SeedRandom[5]; Table[Random[Real, {-1, 1}], {n}]) Now look: In[6]:= F[3] Out[6]= {0.573197,0.696679,0.225655} In[7]:= F[5] Out[7]= {0.573197,0.696679,0.225655,-0.203925,-0.523976} Each "random" sequence will be an "extension" of shorter ones. Andrzej Kozlowski Yokohama, Japan http://www.mimuw.edu.pl/~akoz/ http://platon.c.u-tokyo.ac.jp/andrzej/