# How can I get new random values with each blank in ReplaceAll?

Is there any way to get different values for different blanks with ReplaceAll[]? For example, I would like to do something like:

{_,_,_}/. _Blank -> RandomInteger[{1, 10}]


But, the output comes out as all the same random integer for each blank, such as:

{4, 4, 4}


Is there an efficient way to get an output that is more random, like:

{4, 7, 1}


,as shown above?

Thanks!

• I suppose something like Replace[{_, _, _}, _ :> RandomInteger[{1, 10}], {1}] would do what you ask for, but why not just use RandomInteger[{1,10}, 3] instead? Sep 13, 2018 at 0:06
• Thanks, that's a good idea - I should have been more specific though, because it wasn't just for {,,}, but also for variable lengths, such as {,}, or even {,{,}} and such Sep 13, 2018 at 15:50

You can simply change the Rule -> to RuleDelayed :> to give

{_, _, _} /. _Blank :> RandomInteger[{1, 10}]

(*{5, 1, 3}*)

• Awesome, thanks! Sep 13, 2018 at 14:34

Just for variety: you can also temporarily redefine Blank as RandomInteger[{1,10}]& using Block:

Block[{Blank = RandomInteger[{1, 10}] &}, {_, _, x, y, _, z}]


{5, 7, x, y, 5, z}

• Thanks, I appreciate it! Sep 13, 2018 at 14:34