# How can I permute two delayed variables?

Two variables with delayed expression are used for a random number generation:

Disr1 := RandomVariate[NormalDistribution[0,1]]
Disr2 := RandomVariate[UniformDistribution[{0,1}]]


It is necessary to permute the variable so that Dist1 takes the delayed value, RandomVariate[UniformDistribution[{0,1}]], Dist2---RandomVariate[NormalDistribution[0,1]].

This can be performed straightforwardly:

Disr1 := RandomVariate[UniformDistribution[{0,1}]]
Disr2 := RandomVariate[NormalDistribution[0,1]]


But for some reasons it is desirable a way without using initial right hands.

• You could obviously have a third function and swap the definitions. Please explain why exactly you would need to do something like this and what problem you're really trying to solve. At the moment I find your question quite unclear. Jun 5 at 13:09

The other answer already shows the correct way to do this. For purely academic purposes, here's how you can actually swap definitions of two symbols:

ClearAll[disr1, disr2]
disr1 := RandomVariate[NormalDistribution[10, 1]];
disr2 := RandomVariate[UniformDistribution[{0, 1}]];

disr1
disr2

Module[{temp},
temp = OwnValues[disr1];
OwnValues[disr1] = OwnValues[disr2] /. HoldPattern[disr2] :> disr1;
OwnValues[disr2] = temp /. HoldPattern[disr1] :> disr2
]

disr1
disr2


As you can see, this is rather tedious and error-prone. I made several failed attempts before getting to this "solution". This method of programming should be strongly discouraged.

Something like the following might be what you need. This is a bit arbitrary, but I don't know anything about your context, so you should probably adapt this to make it semantically cleaner.

Disr := RandomVariate[NormalDistribution[0, 1]];
Disr := RandomVariate[UniformDistribution[{0, 1}]]


Now, "permuting" them is just a matter of choosing 1 or 2. This choice could even be random:

Disr /@ RandomChoice[{1, 2}, 10]


While we're guessing at the OP's use-case, I suppose one is that we have the symbols in the middle of a computation and cannot literally write the equations.

swapOwnValues // ClearAll;
swapOwnValues // Attributes = {HoldAll};
swapOwnValues[a_Symbol, b_Symbol] :=
#2 /. Hold[r_] :> SetDelayed[#1, r] &,
{{Unevaluated[a], Unevaluated[b]},
{Hold[b] /. OwnValues[b], Hold[a] /. OwnValues[a]}
}
];

ClearAll[disr1, disr2]
disr1 := RandomVariate[NormalDistribution[10, 1]];
disr2 := RandomVariate[UniformDistribution[{0, 1}]];
swapOwnValues[disr1, disr2];
{OwnValues[disr1], OwnValues[disr2]} // Print;
(*
{{HoldPattern[disr1]:>RandomVariate[UniformDistribution[{0,1}]]},
{HoldPattern[disr2]:>RandomVariate[NormalDistribution[10,1]]}}
*)

• yeah, we really need to know what "permute the variable" actually means. Jun 5 at 20:58
• @lericr Hopefully it's not permuteDefs[{f, g, h}, {3, 1, 2}]. :) Jun 5 at 21:18