title: Lua: Returning rainbow text without adjacent repeats
body: <p>I'm having a bit of difficulty sussing out why the function below isn't producing the results I need. The first function produces results, but not in the manner I expect it to. The second function produces a result, but it's not working in the manner of the first function.  </p>
  
  <p>What I'm trying to do is return a string with random color codes injected to produce a "rainbow" of text. In the past, I was able to do this with xterm, so I thought to modify my original function to suit my current needs. Here are the functions I've tried, and their results.</p>
  
  <pre><code>function rbow(text)
      local colorTable = {"@w", "@b", "@r", "@g", "@y", "@c", "@m", "@W", "@B", "@R", "@G", "@Y", "@C", "@M", "@D"}
  
      local rcolor, sbyte, lastColor, rtext = 0, 1, 0, ""
  
      for i = 1, #text do
          local tempTable = {}
          for _,v in ipairs(colorTable) do
              table.insert(tempTable, v)
          end
  
          if i &gt; 1 then
              table.remove(tempTable, rcolor)
          end
  
          math.randomseed(os.time() * string.byte(rcolor) * sbyte)
          sbyte = string.byte(i)
          rcolor = math.random(1, #tempTable)
          rtext = rtext .. tempTable[rcolor] .. text:sub(i,i)
      end
  
      return rtext
  end
  
  function rbow2(text)
      local colorTable = {"@w", "@b", "@r", "@g", "@y", "@c", "@m", "@W", "@B", "@R", "@G", "@Y", "@C", "@M", "@D"}
      local rcolor, lastColor, sbyte, rtext = 0, 0, 1, ""
  
      for i = 1, #text do
          math.randomseed(os.time() * string.byte(rcolor) * sbyte)
          sbyte = string.byte(i)
          rcolor = math.random(1, #colorTable)
  
          local colorCheck = function()
              if rcolor ~= lastColor then
                  rtext = rtext .. tempTable[rcolor] .. text:sub(i,i)
              else
                  rcolor = math.random(1, #colorTable)
                  colorCheck()
              end
          end
      end
  
      return rtext
  end
  
  print(rbow("This is a long string to see if there are any repeated colors of letters adjacent to each other."))
  
  &gt; @bT@Gh@Gi@ys@Y @gi@Cs@D @ya@C @wl@bo@bn@bg@b @bs@bt@br@bi@bn@Gg@G @Gt@Go@G @Gs@Ge@Ge@G @Gi@yf@B @ct@Mh@ye@Br@ce@M @ya@Br@Be@B @Ba@Bn@By@B @Br@Be@Bp@Be@Ga@gt@Ye@gd@Y @gc@Yo@gl@Yo@gr@Cs@b @co@gf@C @bl@ce@gt@Ct@be@Rr@Ys@D @Da@Dd@Dj@Da@Dc@De@Dn@yt@G @yt@Go@y @Ge@ya@Gc@yh@G @yo@Ct@yh@Ce@yr@C.
  
  
   print(rbow2("This is a long string to see if there are any repeated colors of letters adjacent to each other."))
  
  &gt; 0
  </code></pre>
  
  <p>In the first example, adjacent letters are being represented by the same color code, for example:</p>
  
  <pre><code>@bo@bn@bg@b @bs@bt@br@bi@bn -- all @b
  @G @Gt@Go@G @Gs@Ge@Ge@G @Gi -- all @G
  </code></pre>
  
  <p>and so forth. The odd factor is that the same pattern (with different color codes) pretty much repeats on subsequent returns. It doesn't match exactly, but in three prints, "to see" and "adjacent" carried the same color code throughout the words.</p>
  
  <p>I'm unsure how to proceed with this. I do NOT want it to repeat colors in adjacent letters, and I'd have thought the first function would take care of that, removing the recently used color code from the tempTable. What am I missing?</p>