Re: need help
- To: mathgroup at smc.vnet.net
- Subject: [mg87357] Re: need help
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Tue, 8 Apr 2008 07:15:53 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <ftcoug$ksr$1@smc.vnet.net>
haitomi wrote:
> I need help with two pure functions
>
> 1) Li[Ranlist,x] that return a list of x element selected at random from Ranlist. (Is there any way to gennerate random from Ranlist? For example I have RanList=Range[-100,100])
<snip>
Hi Tomi,
I am not sure to have understood what you are looking for, but I hope
the following will help.
First, note that if one wants to get a list of, say ten, integers within
the range [-100, 100], one can use RandomInteger as in
RandomInteger[{-100, 100}, 10]
{-60, -36, -40, -45, 6, 26, -84, 2, -45, 77}
There exists some other functions such as RandomReal or RandomComplex
too. If one wants to randomly pick out some symbols, one can use
RandomChoice[{a, b, c}, 10]
{c, a, b, b, c, c, c, b, b, c}
Now if you prefer to write your own function, you could use something
like the following:
f1[lst_, n_?NonNegative] /; n <= Length[lst] :=
lst[[RandomInteger[{1, Length[lst]}, n]]]
RanList = Range[-10, 10]
{-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10}
(* We randomly pick out five integer values out of twenty. *)
f1[RanList, 5]
{-3, 7, -3, 9, 1}
Regards,
-- Jean-Marc