MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Random Trouble

  • To: mathgroup at smc.vnet.net
  • Subject: [mg39207] RE: [mg39178] Random Trouble
  • From: Rob Pratt <rpratt at email.unc.edu>
  • Date: Mon, 3 Feb 2003 01:10:55 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On Sun, 2 Feb 2003, Donald A. 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.
>
> Try using = instead of :=.
>
> r = Random[];
>
> Then use r throughout the program.  Using r := Random[] (or just Random[])
> instead would cause a fresh random number to be sampled each time r appears.
>
> Rob Pratt
> Department of Operations Research
> The University of North Carolina at Chapel Hill
>
> rpratt at email.unc.edu
>
> http://www.unc.edu/~rpratt/
>
> Rob - This doesn't work for me.  If I try
>
> F[n_]=Table[Random[Real,{-1,1}],{n}]
>
> I get a different sequence for F[5] each time I use it.
>
> Don Darling


The code I suggested does work for a single random number.

Your command to obtain and remember a random n-vector yields an error
message.  Try the code below.  The first call to F[5] assigns a random
5-vector to F[5].  Subsequent calls to F[5] just look up the stored value.

In[1]:= F[n_] := F[n] = Table[Random[Real, {-1, 1}], {n}]

In[2]:= F[5]

Out[2]= {0.601045, -0.776375, -0.269912, -0.172019, -0.589347}

In[3]:= F[5]

Out[3]= {0.601045, -0.776375, -0.269912, -0.172019, -0.589347}


Rob Pratt
Department of Operations Research
The University of North Carolina at Chapel Hill

rpratt at email.unc.edu

http://www.unc.edu/~rpratt/



  • Prev by Date: Re: Re: Writing a program to hunt for a prime between n^2 and (n+1)^2
  • Next by Date: Re: Re: Writing a program to hunt for a prime between n^2 and (n+1)^2
  • Previous by thread: RE: Random Trouble
  • Next by thread: Re: Random Trouble