Re: Assign a value to a variable
- To: mathgroup at smc.vnet.net
- Subject: [mg63030] Re: Assign a value to a variable
- From: bghiggins at ucdavis.edu
- Date: Sun, 11 Dec 2005 04:57:06 -0500 (EST)
- References: <dnedkh$tf$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Ben,
The following function appeared on the news group some time ago that I
find useful for randomly shuffling a list (unfortunately I cannot
recall the name of the author):
shuffle [lst1_] :=
Module[{ newj, lst = lst1, n},
n = Length[lst];
Do[
newj = Random[Integer, {j, n}];
lst[[{j, newj}]] = lst[[{newj, j}]],
{j, n - 1}]; lst ]
Then it is a simple matter to do the assignment:
vars={a,b,c,d,e,f,g,h,i};
Evaluate[vars] = shuffle[Range[9]]
{2, 9, 6, 4, 7, 5, 1, 3, 8}
Cheers,
Brian
Ben Becker wrote:
> Hi!
>
> I have a list of variables:
> vars = {a, b, c, d, e, f, g, h, i}
>
> and a list of numbers:
> numbers = Range[9]
>
> Now i'd like to choose randomly a variable and a number. and then i'd
> like to assign the number to the variable.
> I thought of somehting like that:
>
> vars[[Random[Integer, {1, Length[vars]}]]] =
> numbers[[Random[Integer, {1, Length[numbers]}]]]
>
> but it doesn't work.
>
> i hope you have an idea!
>
> thank you for answering!
>
> MFG Ben