I am trying to find a way to create an association (preferably without iterating) with values dependent on other values in the association.

I know that this is incorrect, but an attempt that I've made is

assoc = <|"A" -> RandomInteger[{1, 10}], "B" -> 10 - assoc[["A"]]|>

So I just want A to be a RandomInteger between 1 and 10, and then B be the remainder up to 10.

Is this possible?


1 Answer 1


I would write this as

assoc = <|"A" -> #, "B" -> 10 - #|>& @ RandomInteger[{1, 10}]
(* <|"A" -> 9, "B" -> 1|> *)

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.