1
$\begingroup$

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.

$\endgroup$
1
  • 2
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    Commented Jun 5, 2023 at 13:09

3 Answers 3

6
$\begingroup$

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.

$\endgroup$
4
$\begingroup$

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[1] := RandomVariate[NormalDistribution[0, 1]];
Disr[2] := 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]
$\endgroup$
3
$\begingroup$

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] :=
  MapThread[
   #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]]}}
*)
$\endgroup$
2
  • 1
    $\begingroup$ yeah, we really need to know what "permute the variable" actually means. $\endgroup$
    – lericr
    Commented Jun 5, 2023 at 20:58
  • $\begingroup$ @lericr Hopefully it's not permuteDefs[{f, g, h}, {3, 1, 2}]. :) $\endgroup$
    – Michael E2
    Commented Jun 5, 2023 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.