MathGroup Archive 2007

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

Search the Archive

Re: Question about RandomInteger

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83179] Re: [mg83153] Question about RandomInteger
  • From: Darren Glosemeyer <darreng at wolfram.com>
  • Date: Wed, 14 Nov 2007 04:43:32 -0500 (EST)
  • References: <200711130838.DAA27746@smc.vnet.net>

John wrote:
> Needs["MultivariateStatistics`"]
> n=1000
> pdpd={1/6,1/6,1/6,1/6,1/6,1/6}
> pdid={1/7,1/5,1/6,1/6.1/6,33/210}
> u=RandomInteger[MultinomialDistribution[n,pdpd],1]
> v=RandomInteger[MultinomialDistribution[n,pdid],1]
>
> All of the above works.
>
> But u and v appear more than once in subsequent calculations, and
> RandomInteger reexecutes at each appearance. The reexecutions change
> the values of u and v, and that is not what I want to happen.
>
> Any advice will be appreciated.
>
> John
>
>   

As defined, RandomInteger will not be re-evaluated each time u and v are 
used within the same session.


In[1]:= Needs["MultivariateStatistics`"]

In[2]:= n = 1000;

In[3]:= pdpd = {1/6, 1/6, 1/6, 1/6, 1/6, 1/6};

In[4]:= pdid = {1/7, 1/5, 1/6, 1/6, 1/6, 33/210};

In[5]:= u = RandomInteger[MultinomialDistribution[n, pdpd], 1]

Out[5]= {{155, 188, 170, 174, 162, 151}}

In[6]:= v = RandomInteger[MultinomialDistribution[n, pdid], 1]

Out[6]= {{133, 220, 167, 176, 170, 134}}


Here we see that u and v do not change when evaluated.

In[7]:= {u, u, u}

Out[7]= {{{155, 188, 170, 174, 162, 151}}, {{155, 188, 170, 174, 162, 
151}},
 
 >    {{155, 188, 170, 174, 162, 151}}}

In[8]:= {v, v, v}

Out[8]= {{{133, 220, 167, 176, 170, 134}}, {{133, 220, 167, 176, 170, 
134}},
 
 >    {{133, 220, 167, 176, 170, 134}}}


My best guess, without seeing the actual example where re-evaluation 
occurs, is that either the entire assignment is re-evaluated in the code 
(e.g. the subsequent calculations use u = 
RandomInteger[MultinomialDistribution[n, pdpd], 1] rather than just u), 
or the definitions actually used SetDelayed (:=) rather than Set (=). 
With a SetDelayed definition the right-hand side of the definition will 
be re-evaluated each time the left-hand side is used.


In[9]:= u2 := RandomInteger[MultinomialDistribution[n, pdpd], 1]

In[10]:= v2 := RandomInteger[MultinomialDistribution[n, pdpd], 1]

In[11]:= {u2, u2, u2}

Out[11]= {{{154, 145, 185, 172, 170, 174}}, {{173, 166, 142, 184, 188, 
147}},
 
 >    {{177, 164, 172, 152, 168, 167}}}

In[12]:= {v2, v2, v2}

Out[12]= {{{174, 171, 167, 165, 177, 146}}, {{186, 160, 165, 175, 166, 
148}},
 
 >    {{150, 163, 173, 178, 166, 170}}}


If consistent values are needed across sessions or there is a need to 
re-evaluate the assignments to u and v in your application, BlockRandom 
and SeedRandom can be used as others have suggested.


Darren Glosemeyer
Wolfram Research


  • Prev by Date: DelaunayTriangulation[] output
  • Next by Date: Re: Partial Differentiation of Implicit Functions
  • Previous by thread: Re: Question about RandomInteger
  • Next by thread: Re: Question about RandomInteger