dimanche 2 mai 2021

Lua how to keep the same percentage after randomseed is called?

I tried to make a random tic tac toe program start with a different move every time, using randomseed and a function that shuffles the table of moves, but then the fixed winning percentages (~43% for both X and O and the rest is for ties) became randomized, too (to the point where one of the signs usually gets 100% or 99.99% of the victories).

Is there a way to keep the percentages the way they used to be before I called randomseed, while still having randomseed active so the table will be randomly shuffled, and the first move will be unique for every round and iteration?

The parts that I changed are in the function that creates a new game (before the actual turns begin):

game.turn = math.random(X,O)

game.winner = nil

game.iMove = 0

math.randomseed(os.time())
math.random() math.random() math.random() -- Works the same with and without those.

And the function that runs every turn:

local nextMove = table.remove(shuffle(game.moves),math.random(#game.moves))

When shuffle is the standard:

function shuffle(tbl)
   for i = #tbl, 2, -1 do
      local j = math.random(i)
      tbl[i], tbl[j] = tbl[j], tbl[i]
   end
 return tbl
 end

No other function in the program calls math.random, so I figured that these bits of code may be the ones where the issue could've occurred in.




Aucun commentaire:

Enregistrer un commentaire