Generate random color in way that works with both pdflatex and lualatex

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












Right now, I am generating a random color in pdftex using



definecolorrandomcolorRGB

pdfuniformdeviate 255,
pdfuniformdeviate 255,
pdfuniformdeviate 255



Which works pretty well,
until I try and compile with luatex.



What is the nicest way to generate random colors (either directly or via generating 3 random numbers), that is compatible across both pdftex and luatex?



I suspect something involving pgf is the answer.
Or maybe just a condition with one branch for pdftex (that looks like the current), and one for luatex (that maybe invokes a tiny snippet of luacode).










share|improve this question

















  • 2




    Is the luatex85 package usable? It brings back all the deleted pdf... macros to lualatex
    – daleif
    Aug 21 at 11:06










  • @daleif yes. Expand that into an answer, with a link to the docs maybe and that would be great. Assuming luatex85 is a no-op or something in pdftex
    – Lyndon White
    Aug 21 at 11:14














up vote
7
down vote

favorite












Right now, I am generating a random color in pdftex using



definecolorrandomcolorRGB

pdfuniformdeviate 255,
pdfuniformdeviate 255,
pdfuniformdeviate 255



Which works pretty well,
until I try and compile with luatex.



What is the nicest way to generate random colors (either directly or via generating 3 random numbers), that is compatible across both pdftex and luatex?



I suspect something involving pgf is the answer.
Or maybe just a condition with one branch for pdftex (that looks like the current), and one for luatex (that maybe invokes a tiny snippet of luacode).










share|improve this question

















  • 2




    Is the luatex85 package usable? It brings back all the deleted pdf... macros to lualatex
    – daleif
    Aug 21 at 11:06










  • @daleif yes. Expand that into an answer, with a link to the docs maybe and that would be great. Assuming luatex85 is a no-op or something in pdftex
    – Lyndon White
    Aug 21 at 11:14












up vote
7
down vote

favorite









up vote
7
down vote

favorite











Right now, I am generating a random color in pdftex using



definecolorrandomcolorRGB

pdfuniformdeviate 255,
pdfuniformdeviate 255,
pdfuniformdeviate 255



Which works pretty well,
until I try and compile with luatex.



What is the nicest way to generate random colors (either directly or via generating 3 random numbers), that is compatible across both pdftex and luatex?



I suspect something involving pgf is the answer.
Or maybe just a condition with one branch for pdftex (that looks like the current), and one for luatex (that maybe invokes a tiny snippet of luacode).










share|improve this question













Right now, I am generating a random color in pdftex using



definecolorrandomcolorRGB

pdfuniformdeviate 255,
pdfuniformdeviate 255,
pdfuniformdeviate 255



Which works pretty well,
until I try and compile with luatex.



What is the nicest way to generate random colors (either directly or via generating 3 random numbers), that is compatible across both pdftex and luatex?



I suspect something involving pgf is the answer.
Or maybe just a condition with one branch for pdftex (that looks like the current), and one for luatex (that maybe invokes a tiny snippet of luacode).







pdftex color luatex random






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 21 at 10:55









Lyndon White

1,3941129




1,3941129







  • 2




    Is the luatex85 package usable? It brings back all the deleted pdf... macros to lualatex
    – daleif
    Aug 21 at 11:06










  • @daleif yes. Expand that into an answer, with a link to the docs maybe and that would be great. Assuming luatex85 is a no-op or something in pdftex
    – Lyndon White
    Aug 21 at 11:14












  • 2




    Is the luatex85 package usable? It brings back all the deleted pdf... macros to lualatex
    – daleif
    Aug 21 at 11:06










  • @daleif yes. Expand that into an answer, with a link to the docs maybe and that would be great. Assuming luatex85 is a no-op or something in pdftex
    – Lyndon White
    Aug 21 at 11:14







2




2




Is the luatex85 package usable? It brings back all the deleted pdf... macros to lualatex
– daleif
Aug 21 at 11:06




Is the luatex85 package usable? It brings back all the deleted pdf... macros to lualatex
– daleif
Aug 21 at 11:06












@daleif yes. Expand that into an answer, with a link to the docs maybe and that would be great. Assuming luatex85 is a no-op or something in pdftex
– Lyndon White
Aug 21 at 11:14




@daleif yes. Expand that into an answer, with a link to the docs maybe and that would be great. Assuming luatex85 is a no-op or something in pdftex
– Lyndon White
Aug 21 at 11:14










3 Answers
3






active

oldest

votes

















up vote
7
down vote



accepted










The luatex85 package does



letpdfuniformdeviateuniformdeviate


You can thus do usepackageluatex85 or



documentclassarticle
usepackagexcolor

unlessifdefinedpdfuniformdeviate
letpdfuniformdeviateuniformdeviate
fi

definecolorrandomcolorRGB

pdfuniformdeviate 255,
pdfuniformdeviate 255,
pdfuniformdeviate 255

extractcolorspecrandomcolortest
typeouttest

stop


This will type out something like



rgb0.84706,0.4,0.68234


with both pdflatex or lualatex. I guess that, when random numbers will be available also in xelatex, a common interface will be added to the LaTeX kernel.






share|improve this answer



























    up vote
    4
    down vote














    Based on Randomly assign background colour for each frame




    Another approach based on pgf:



    documentclassarticle

    usepackagepgf
    usepackagepgffor
    usepackagexcolor

    usepackageifluatex
    usepackageifxetex

    ifluatex
    letpdfrandomseedrandomseed
    fi

    ifxetex
    pgfmathsetseedtime
    else
    pgfmathsetseednumberpdfrandomseed
    fi

    newcommandrandomcolor%
    pgfmathsetmacroRrnd%
    pgfmathsetmacroGrnd%
    pgfmathsetmacroBrnd%
    definecolorrandomcolrgbR,G,B%


    begindocument

    noindent%
    foreach n in 1,...,1350%
    randomcolorcolorrandomcolrule0.41cm0.41cm-%


    enddocument


    enter image description here






    share|improve this answer



























      up vote
      4
      down vote













      There is also the lcg package (short for Linear Congruential Generator), that works with all compilers, including lualatex. The package provides a command rand to generate a random number and store it in the rand counter. For use in definecolor you should convert the counter with thevalue.



      You can set the bounds of the random generator with the package options first and last, and optionally change them within the document using the reinitrand command. The default seed is based on the system clock, using the minutes as unit (so the output will change only once per minute). You can also provide a custom seed.



      MWE:



      documentclassarticle
      usepackage[first=1,last=255]lcg
      usepackagexcolor
      randedefcolorathevaluerand
      randedefcolorbthevaluerand
      randedefcolorcthevaluerand

      definecolorrandomcolorRGB

      colora,
      colorb,
      colorc

      begindocument
      colorboxrandomcolorRandom color: colora,colorb,colorc
      enddocument


      Result:



      enter image description here






      share|improve this answer


















      • 1




        Use thevaluerand instead of arabic. arabic is a formatting command and there is no garanty that it gives a number (e.g. hebrew and arabic packages often redefine it).
        – Ulrike Fischer
        Aug 21 at 19:08










      • @UlrikeFischer thanks for the improvement, it didn't occur to me to put the there - edited.
        – Marijn
        Aug 23 at 11:42










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "85"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f446944%2fgenerate-random-color-in-way-that-works-with-both-pdflatex-and-lualatex%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      7
      down vote



      accepted










      The luatex85 package does



      letpdfuniformdeviateuniformdeviate


      You can thus do usepackageluatex85 or



      documentclassarticle
      usepackagexcolor

      unlessifdefinedpdfuniformdeviate
      letpdfuniformdeviateuniformdeviate
      fi

      definecolorrandomcolorRGB

      pdfuniformdeviate 255,
      pdfuniformdeviate 255,
      pdfuniformdeviate 255

      extractcolorspecrandomcolortest
      typeouttest

      stop


      This will type out something like



      rgb0.84706,0.4,0.68234


      with both pdflatex or lualatex. I guess that, when random numbers will be available also in xelatex, a common interface will be added to the LaTeX kernel.






      share|improve this answer
























        up vote
        7
        down vote



        accepted










        The luatex85 package does



        letpdfuniformdeviateuniformdeviate


        You can thus do usepackageluatex85 or



        documentclassarticle
        usepackagexcolor

        unlessifdefinedpdfuniformdeviate
        letpdfuniformdeviateuniformdeviate
        fi

        definecolorrandomcolorRGB

        pdfuniformdeviate 255,
        pdfuniformdeviate 255,
        pdfuniformdeviate 255

        extractcolorspecrandomcolortest
        typeouttest

        stop


        This will type out something like



        rgb0.84706,0.4,0.68234


        with both pdflatex or lualatex. I guess that, when random numbers will be available also in xelatex, a common interface will be added to the LaTeX kernel.






        share|improve this answer






















          up vote
          7
          down vote



          accepted







          up vote
          7
          down vote



          accepted






          The luatex85 package does



          letpdfuniformdeviateuniformdeviate


          You can thus do usepackageluatex85 or



          documentclassarticle
          usepackagexcolor

          unlessifdefinedpdfuniformdeviate
          letpdfuniformdeviateuniformdeviate
          fi

          definecolorrandomcolorRGB

          pdfuniformdeviate 255,
          pdfuniformdeviate 255,
          pdfuniformdeviate 255

          extractcolorspecrandomcolortest
          typeouttest

          stop


          This will type out something like



          rgb0.84706,0.4,0.68234


          with both pdflatex or lualatex. I guess that, when random numbers will be available also in xelatex, a common interface will be added to the LaTeX kernel.






          share|improve this answer












          The luatex85 package does



          letpdfuniformdeviateuniformdeviate


          You can thus do usepackageluatex85 or



          documentclassarticle
          usepackagexcolor

          unlessifdefinedpdfuniformdeviate
          letpdfuniformdeviateuniformdeviate
          fi

          definecolorrandomcolorRGB

          pdfuniformdeviate 255,
          pdfuniformdeviate 255,
          pdfuniformdeviate 255

          extractcolorspecrandomcolortest
          typeouttest

          stop


          This will type out something like



          rgb0.84706,0.4,0.68234


          with both pdflatex or lualatex. I guess that, when random numbers will be available also in xelatex, a common interface will be added to the LaTeX kernel.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 21 at 11:32









          egreg

          683k8418223067




          683k8418223067




















              up vote
              4
              down vote














              Based on Randomly assign background colour for each frame




              Another approach based on pgf:



              documentclassarticle

              usepackagepgf
              usepackagepgffor
              usepackagexcolor

              usepackageifluatex
              usepackageifxetex

              ifluatex
              letpdfrandomseedrandomseed
              fi

              ifxetex
              pgfmathsetseedtime
              else
              pgfmathsetseednumberpdfrandomseed
              fi

              newcommandrandomcolor%
              pgfmathsetmacroRrnd%
              pgfmathsetmacroGrnd%
              pgfmathsetmacroBrnd%
              definecolorrandomcolrgbR,G,B%


              begindocument

              noindent%
              foreach n in 1,...,1350%
              randomcolorcolorrandomcolrule0.41cm0.41cm-%


              enddocument


              enter image description here






              share|improve this answer
























                up vote
                4
                down vote














                Based on Randomly assign background colour for each frame




                Another approach based on pgf:



                documentclassarticle

                usepackagepgf
                usepackagepgffor
                usepackagexcolor

                usepackageifluatex
                usepackageifxetex

                ifluatex
                letpdfrandomseedrandomseed
                fi

                ifxetex
                pgfmathsetseedtime
                else
                pgfmathsetseednumberpdfrandomseed
                fi

                newcommandrandomcolor%
                pgfmathsetmacroRrnd%
                pgfmathsetmacroGrnd%
                pgfmathsetmacroBrnd%
                definecolorrandomcolrgbR,G,B%


                begindocument

                noindent%
                foreach n in 1,...,1350%
                randomcolorcolorrandomcolrule0.41cm0.41cm-%


                enddocument


                enter image description here






                share|improve this answer






















                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote










                  Based on Randomly assign background colour for each frame




                  Another approach based on pgf:



                  documentclassarticle

                  usepackagepgf
                  usepackagepgffor
                  usepackagexcolor

                  usepackageifluatex
                  usepackageifxetex

                  ifluatex
                  letpdfrandomseedrandomseed
                  fi

                  ifxetex
                  pgfmathsetseedtime
                  else
                  pgfmathsetseednumberpdfrandomseed
                  fi

                  newcommandrandomcolor%
                  pgfmathsetmacroRrnd%
                  pgfmathsetmacroGrnd%
                  pgfmathsetmacroBrnd%
                  definecolorrandomcolrgbR,G,B%


                  begindocument

                  noindent%
                  foreach n in 1,...,1350%
                  randomcolorcolorrandomcolrule0.41cm0.41cm-%


                  enddocument


                  enter image description here






                  share|improve this answer













                  Based on Randomly assign background colour for each frame




                  Another approach based on pgf:



                  documentclassarticle

                  usepackagepgf
                  usepackagepgffor
                  usepackagexcolor

                  usepackageifluatex
                  usepackageifxetex

                  ifluatex
                  letpdfrandomseedrandomseed
                  fi

                  ifxetex
                  pgfmathsetseedtime
                  else
                  pgfmathsetseednumberpdfrandomseed
                  fi

                  newcommandrandomcolor%
                  pgfmathsetmacroRrnd%
                  pgfmathsetmacroGrnd%
                  pgfmathsetmacroBrnd%
                  definecolorrandomcolrgbR,G,B%


                  begindocument

                  noindent%
                  foreach n in 1,...,1350%
                  randomcolorcolorrandomcolrule0.41cm0.41cm-%


                  enddocument


                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 21 at 21:23









                  samcarter

                  75.8k786242




                  75.8k786242




















                      up vote
                      4
                      down vote













                      There is also the lcg package (short for Linear Congruential Generator), that works with all compilers, including lualatex. The package provides a command rand to generate a random number and store it in the rand counter. For use in definecolor you should convert the counter with thevalue.



                      You can set the bounds of the random generator with the package options first and last, and optionally change them within the document using the reinitrand command. The default seed is based on the system clock, using the minutes as unit (so the output will change only once per minute). You can also provide a custom seed.



                      MWE:



                      documentclassarticle
                      usepackage[first=1,last=255]lcg
                      usepackagexcolor
                      randedefcolorathevaluerand
                      randedefcolorbthevaluerand
                      randedefcolorcthevaluerand

                      definecolorrandomcolorRGB

                      colora,
                      colorb,
                      colorc

                      begindocument
                      colorboxrandomcolorRandom color: colora,colorb,colorc
                      enddocument


                      Result:



                      enter image description here






                      share|improve this answer


















                      • 1




                        Use thevaluerand instead of arabic. arabic is a formatting command and there is no garanty that it gives a number (e.g. hebrew and arabic packages often redefine it).
                        – Ulrike Fischer
                        Aug 21 at 19:08










                      • @UlrikeFischer thanks for the improvement, it didn't occur to me to put the there - edited.
                        – Marijn
                        Aug 23 at 11:42














                      up vote
                      4
                      down vote













                      There is also the lcg package (short for Linear Congruential Generator), that works with all compilers, including lualatex. The package provides a command rand to generate a random number and store it in the rand counter. For use in definecolor you should convert the counter with thevalue.



                      You can set the bounds of the random generator with the package options first and last, and optionally change them within the document using the reinitrand command. The default seed is based on the system clock, using the minutes as unit (so the output will change only once per minute). You can also provide a custom seed.



                      MWE:



                      documentclassarticle
                      usepackage[first=1,last=255]lcg
                      usepackagexcolor
                      randedefcolorathevaluerand
                      randedefcolorbthevaluerand
                      randedefcolorcthevaluerand

                      definecolorrandomcolorRGB

                      colora,
                      colorb,
                      colorc

                      begindocument
                      colorboxrandomcolorRandom color: colora,colorb,colorc
                      enddocument


                      Result:



                      enter image description here






                      share|improve this answer


















                      • 1




                        Use thevaluerand instead of arabic. arabic is a formatting command and there is no garanty that it gives a number (e.g. hebrew and arabic packages often redefine it).
                        – Ulrike Fischer
                        Aug 21 at 19:08










                      • @UlrikeFischer thanks for the improvement, it didn't occur to me to put the there - edited.
                        – Marijn
                        Aug 23 at 11:42












                      up vote
                      4
                      down vote










                      up vote
                      4
                      down vote









                      There is also the lcg package (short for Linear Congruential Generator), that works with all compilers, including lualatex. The package provides a command rand to generate a random number and store it in the rand counter. For use in definecolor you should convert the counter with thevalue.



                      You can set the bounds of the random generator with the package options first and last, and optionally change them within the document using the reinitrand command. The default seed is based on the system clock, using the minutes as unit (so the output will change only once per minute). You can also provide a custom seed.



                      MWE:



                      documentclassarticle
                      usepackage[first=1,last=255]lcg
                      usepackagexcolor
                      randedefcolorathevaluerand
                      randedefcolorbthevaluerand
                      randedefcolorcthevaluerand

                      definecolorrandomcolorRGB

                      colora,
                      colorb,
                      colorc

                      begindocument
                      colorboxrandomcolorRandom color: colora,colorb,colorc
                      enddocument


                      Result:



                      enter image description here






                      share|improve this answer














                      There is also the lcg package (short for Linear Congruential Generator), that works with all compilers, including lualatex. The package provides a command rand to generate a random number and store it in the rand counter. For use in definecolor you should convert the counter with thevalue.



                      You can set the bounds of the random generator with the package options first and last, and optionally change them within the document using the reinitrand command. The default seed is based on the system clock, using the minutes as unit (so the output will change only once per minute). You can also provide a custom seed.



                      MWE:



                      documentclassarticle
                      usepackage[first=1,last=255]lcg
                      usepackagexcolor
                      randedefcolorathevaluerand
                      randedefcolorbthevaluerand
                      randedefcolorcthevaluerand

                      definecolorrandomcolorRGB

                      colora,
                      colorb,
                      colorc

                      begindocument
                      colorboxrandomcolorRandom color: colora,colorb,colorc
                      enddocument


                      Result:



                      enter image description here







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 23 at 11:41

























                      answered Aug 21 at 16:13









                      Marijn

                      6,671532




                      6,671532







                      • 1




                        Use thevaluerand instead of arabic. arabic is a formatting command and there is no garanty that it gives a number (e.g. hebrew and arabic packages often redefine it).
                        – Ulrike Fischer
                        Aug 21 at 19:08










                      • @UlrikeFischer thanks for the improvement, it didn't occur to me to put the there - edited.
                        – Marijn
                        Aug 23 at 11:42












                      • 1




                        Use thevaluerand instead of arabic. arabic is a formatting command and there is no garanty that it gives a number (e.g. hebrew and arabic packages often redefine it).
                        – Ulrike Fischer
                        Aug 21 at 19:08










                      • @UlrikeFischer thanks for the improvement, it didn't occur to me to put the there - edited.
                        – Marijn
                        Aug 23 at 11:42







                      1




                      1




                      Use thevaluerand instead of arabic. arabic is a formatting command and there is no garanty that it gives a number (e.g. hebrew and arabic packages often redefine it).
                      – Ulrike Fischer
                      Aug 21 at 19:08




                      Use thevaluerand instead of arabic. arabic is a formatting command and there is no garanty that it gives a number (e.g. hebrew and arabic packages often redefine it).
                      – Ulrike Fischer
                      Aug 21 at 19:08












                      @UlrikeFischer thanks for the improvement, it didn't occur to me to put the there - edited.
                      – Marijn
                      Aug 23 at 11:42




                      @UlrikeFischer thanks for the improvement, it didn't occur to me to put the there - edited.
                      – Marijn
                      Aug 23 at 11:42

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f446944%2fgenerate-random-color-in-way-that-works-with-both-pdflatex-and-lualatex%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay