1

I want to change the element of an pgf math array within a multido environment where a random element is incremented by 1:

\def\list{0,0,0,0,0,0,0,0,0,0,0}
\multido{\i=0+1}{20}{
   \pgfmathsetmacro{\index}{random(0,10)}
   \pgfmathparse{\list[\index]+1}
   \def\list[\index]{\pgfmathresult}
   (do something with list)
 }

however if I define \list[0] it just defines this as a new variable and the vector \list is lost. Is there a way to change the value of an element of the array?

gaugi
  • 111
  • PGF arrays are not data structures but only macros. So an element is not defined in the eyes of TeX. You have to redefine the list (which you would better use a better macro name such as mylist). You can also use foreach anyways if you are using pgf already via \usepackage{pgffor} – percusse Feb 20 '18 at 12:11
  • Welcome to TeX.SX! Is your aim to build a list consisting of 21 random integers? – egreg Feb 20 '18 at 12:25
  • Hi, my aim is to keep those 10 integers, but to increment one randomly chosen integer by one each loop. Would it be possible to maybe use tikz-math, with \mylist{\index}=\mylist{\index}+1 ? (I'm struggling to install tikz-math right now, as I don't have root) – gaugi Feb 20 '18 at 14:40
  • 1
    If you want to use an actual array, see https://tex.stackexchange.com/questions/346260/display-item-list-in-sequential-and-random-without-repetition-order/346285?s=2|19.2192#346285 for example. – John Kormylo Feb 21 '18 at 16:47

0 Answers0