Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: Noob evaluation question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg74192] Re: Noob evaluation question
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 14 Mar 2007 03:44:03 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <et54tf$b7m$1@smc.vnet.net>

heycarnut wrote:
> Hi -
> 
> Helping a buddy out with a small application, and I'm drawing a blank
> on the following.
> 
> Given :
> 
>    Position[{1, 2, 3, 4, 5, 6, 7, 8, 9}, _?(# <= Random[Integer, {1,
> 9}] &)]
> 
> How can we make the generated random number 'fixed' for the evaluation
> of Position, instead of a new number getting generated at each element
> of the list (the above is a simplified and contrived example.)
> 
> Thanks
> Rob
> 
> 

One must evaluate the random number outside the iterative expression 
(the function Position in this case). One way of doing this is,

In[1]:=
With[{rnd = Random[Integer, {1, 9}]},
   Print["Generated Random Number : ", rnd];
    Position[{1, 2, 3, 4, 5, 6, 7, 8, 9}, _?(#1 <= rnd & )]]

 From In[1]:=
Generated Random Number : 7

Out[1]=
{{1}, {2}, {3}, {4}, {5}, {6}, {7}}

Regards,
Jean-Marc


  • Prev by Date: Re: Replacement question
  • Next by Date: Re: Interpolating Function
  • Previous by thread: Re: Noob evaluation question
  • Next by thread: Re: Noob evaluation question