2
$\begingroup$

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?

$\endgroup$
2
$\begingroup$

I would write this as

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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