Hi,
What is the correct method to implement the following?
Requirement:- We have an array a of randomly generated integers (RandomInteger[]) of variable length n.
- We compute and display the multiple t*a for a variable t.
- We expect the following behaviour:
- If we change the value of the array length n, then the array a should be newly updated by new random values (and also the output t*a, of course).
- If we change the value of the factor t, then the array a should not change, i.e., no generation of new random variables, and the output should just update the multiple t*a with the new t.
- Of course, any output of the variables t and n should also be updated, if changed.
Basically, I have a problem with behaviour 2., since changing the variable t apparently triggers the random generator to generate new variables.
Examples:This has the correct update behaviour for n, but not for t:
ClearAll["n", "t", "a"]
Manipulate[
Column[{
Row[{Text["n = "], n}],
Row[{Text["t = "], t}],
Row[{Text["a = "], ToString[a]}],
Row[{Text["ta = "], ToString[t a]}]
}],
{{n, 4, "n ="}, 1, 60, 1},
{{t, 2, "t ="}, -20, 20, 0.1},
Initialization :> (
a := Table[RandomInteger[{0, 100}], {i, 1, n}];
)
]
This has the correct update behaviour for t, but not for n:
ClearAll["n", "t", "a"]
Manipulate[
Column[{
Row[{Text["n = "], n}],
Row[{Text["t = "], t}],
Row[{Text["a = "], ToString[a]}],
Row[{Text["ta = "], ToString[t a]}]
}],
{{n, 4, "n ="}, 1, 60, 1},
{{t, 2, "t ="}, -20, 20, 0.1},
Initialization :> (
a = Table[RandomInteger[{0, 100}], {i, 1, n}];
)
]
I tried to figure out how to maybe use BlockRandom[] or Module[], but didn't get it. Somewhat of a workaround is the following, but includes the additional use of a button to explicitely generate newly a:
ClearAll["n", "t", "a"]
Manipulate[
arr = BlockRandom[a];
Column[{
Row[{Text["n = "], n}],
Row[{Text["t = "], t}],
Row[{Text["a = "], ToString[arr]}],
Row[{Text["ta = "], ToString[t arr]}]
}],
{{n, 4, "n ="}, 1, 60, 1},
{{t, 2, "t ="}, -20, 20, 0.1},
Button["Generate new a",
arr = Table[RandomInteger[{0, 100}], {i, 1, n}]],
Initialization :> (
a := Table[RandomInteger[{0, 100}], {i, 1, n}];
)
]
Btw., I don't understand why the button action is only executed every second time I click (any reason for this?).
I am very interested to learn how this functionality would be implemented by a Mathematica expert! Since any further "trial and error" tweaking by me will probably only be a "dirty" workaround :-)
Thank you!
Andreas