0
$\begingroup$

My goal is to append random number to list like below.The problem is whenever I try appending a random number, the previously created numbers are changed which I don't know why. How to add a random number without changing existing number?

Manipulate[ Nest[(Append[#, RandomInteger[10]]) &, {}, i], {i, 1, 5, 1}]}]

enter image description here

enter image description here

※Althogh I tried Nest and Pure function as above, result was happened as the same as code below.

Manipulate[ Table[RandomInteger[10], i], {i, 1, 5, 1}]
$\endgroup$
2
  • 4
    $\begingroup$ Generate first, display later: SeedRandom[1]; With[{num = Nest[(Append[#, RandomInteger[10]]) &, {}, 5]}, Manipulate[num[[1 ;; i]], {i, 1, 5, 1}] ] ? or please elaborate how you intend to use this. $\endgroup$
    – Syed
    Nov 9, 2023 at 14:48
  • 1
    $\begingroup$ Or start with a fixed seed: Manipulate[Nest[(Append[#, RandomInteger[10]]) &, SeedRandom[1]; {}, i], {i, 1, 5, 1}] $\endgroup$
    – Domen
    Nov 9, 2023 at 14:57

2 Answers 2

1
$\begingroup$

If you want to just hide/reveal parts of a randomly generated list, you can do this:

With[
  {listLength = 5},
  With[
    {fullList = RandomInteger[10, listLength]},
    Manipulate[Take[fullList, i], {i, 1, 5, 1}]]]

If you want "real time" randomness, you might do something like this:

DynamicModule[
  {list = {}, maxLength = 5},
  Manipulate[
    list = PadRight[list, len, RandomInteger[10]], 
    {{len, 0}, 0, maxLength, 1}]]
$\endgroup$
0
$\begingroup$

Thank you for your comments;using the random number seed.It was simple matter,which I now realize.

Manipulate[SeedRandom[1]; Table[RandomInteger[10], i], {i, 1, 5, 1}]

$\endgroup$
1
  • 3
    $\begingroup$ But this is just showing more or less of essentially a fixed list. If that's all you wanted, you could have just generated that list beforehand. $\endgroup$
    – lericr
    Nov 10, 2023 at 0:13

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.