10

One more question for today: I'm trying to show two random integers with a plus (+) sign between them, in an unevaluated form. I know how Hold and HoldForm work, but they hold everything, including the RandomInteger:

Hold[RandomInteger[100] + RandomInteger[100]]

I've tried then Evaluate before RandomInteger, but that doesn't seem to do the trick.

Any help with this? Very much appreciated, as always!

Gabriel
  • 1,877
  • 12
  • 22
  • 5
    This is a straight-forward case for the Trott-Strzebonski technique, discussed e.g. here. Apply this rule to your expression: r_RandomInteger :> With[{eval = r}, eval /; True]. The reason Evaluate does not help is that it is too deep for it. – Leonid Shifrin Sep 05 '13 at 17:41

4 Answers4

12
HoldForm[#1 + #2]&[RandomInteger[100], RandomInteger[100]]
 (* 77 + 84 *)
ybeltukov
  • 43,673
  • 5
  • 108
  • 212
11

I propose:

HoldForm[+##] & @@ RandomInteger[100, 2]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • The form +## rates highly on my weirdo meter. Weirdo. BTW, +1. :) – rcollyer Sep 06 '13 at 17:16
  • 1
    @rcollyer Yes, it's a favorite of mine, thank-you-very-much. :D – Mr.Wizard Sep 06 '13 at 17:21
  • Great! Your solution is very compact and can be generalized to any numbers of terms, +1 :) – ybeltukov Sep 06 '13 at 17:33
  • @ybeltukov I'm glad you appreciate it. Thanks for the vote. – Mr.Wizard Sep 06 '13 at 17:37
  • 2
    @Blackbird It is not directly documented that I know of, but it comes from an understanding of Mathematica's parsing. +x parses as Plus[x] as can be seen with Hold[+x] // FullForm. So +## is Plus[##] and then it's just a matter of SlotSequence which is directly documented. As a second example 1 x parses as Times[1, x] so we can use 1 ## as shorthand for multiplying arguments. – Mr.Wizard Sep 06 '13 at 18:22
  • @Mr.Wizard:thanks for replying, I thought I have already finished my quota of stupid questions for today. – Pankaj Sejwal Sep 06 '13 at 18:26
  • @Blackbird Oh s#!t, there's a quota on those now? :o) – Mr.Wizard Sep 06 '13 at 18:28
  • @Mr.Wizard: Ya there is and I am increasing it as well..:p actually one more question , is it possible to write such a short notation for Subtract and Divide, because it doesn't seem to work. – Pankaj Sejwal Sep 06 '13 at 18:34
  • @Blackbird I can answer that with one of my favorite retorts: "It works, just not the way you think it does." Hold[-x] // FullForm shows that -x parses as Times[-1, x], so if you want to multiply and then negate arguments it will work fine. :^) I'm not sure what you tried with for Divide but I figure something like 1/x which parses as Times[1, Power[x, -1]]. That's even stranger as now you will have a power tower ending in -1. Incidentally what multiple-argument form of subtract and divide were you aiming for? I mean, did you want a - b - c - d for example? – Mr.Wizard Sep 06 '13 at 18:40
  • @Mr.Wizard : well if OP had requested for a Divide or Subtract operator between the operands than how you can modify this post with somewhat similar syntactic approach. – Pankaj Sejwal Sep 06 '13 at 18:50
  • @Blackbird Again, do you mean e.g. a - b - c - d and a / b / c / d or something else? – Mr.Wizard Sep 06 '13 at 18:51
  • @Mr.Wizard : yes yes the same. – Pankaj Sejwal Sep 06 '13 at 18:53
  • @Blackbird That gets pretty complicated because of the internal format of those expressions. Basically Mathematica doesn't really use that format: it's purely for input. I need to leave for the day but at another time when we are both here perhaps you would post a question asking about these so that I may answer with more room than comments provide. – Mr.Wizard Sep 06 '13 at 18:55
  • ok I shall post one, thanks for your time. – Pankaj Sejwal Sep 06 '13 at 18:56
3

This way you can hold it too,

Hold[Plus[a, b]] /. {a -> RandomInteger[100], b -> RandomInteger[100]}

Hold[91 + 4]

HoldForm[Plus[a, b]] /. {a -> RandomInteger[100], 
  b -> RandomInteger[100]}

87+22

Read the difference between Hold and HoldForm to know they are very close.

Pankaj Sejwal
  • 2,063
  • 14
  • 23
2

Late to this party, but here's a nice trick that surprisingly works:

Composition[HoldForm, Plus] @@ RandomInteger[100, 2]

OR

Composition[HoldForm, Plus] @@ {RandomInteger[100], RandomInteger[100]}
RunnyKine
  • 33,088
  • 3
  • 109
  • 176