2

I have a 2 dim array and all it's cells filled with zeros.

What i'm trying to do is to take some randomly chosen cells and fill it with 4 or 5

but what i get is either empty gird with all value equal to zero or i get just one value that has changed to 4 or 5 and that's my code below:

     local grid = {}
  for i=1,10 do
    grid[i] = {}
    for j=1,10 do
      grid[i][j] = 0
    end
  end

  local empty={}
  for i=1,10 do
    for j=1,10 do
      if grid[i][j]==0 then
        table.insert(empty,i ..'-'.. j)
      end
    end
  end
  local fp=math.floor(table.maxn(empty)/3)
  local fx,fy
  for i=1,fp do

    math.randomseed(os.time())
    math.random(0,1)
    local fo=math.random(0,1)
    math.random(table.maxn(empty))
    local temp= empty[math.random(table.maxn(empty))]
    local dashindex=string.find(temp,'-')

     fx=tonumber(string.sub(temp,1,dashindex-1))
     fy=tonumber(string.sub(temp,dashindex+1,string.len(temp)))
    if fo==0 then
      grid[fx][fy]=4
    elseif fo==1 then
      grid[fx][fy]=5
    end
end


for i=1,10 do
  for j=1,10 do
    print(grid[i][j])
  end
  print('\n')
end
Antwan
  • 3,837
  • 9
  • 41
  • 62
  • when i debug my code i get a correct result but when i run it i don't and i print out the fx and fy each time and i get that they are the same each time – Antwan Jun 19 '14 at 09:16
  • `math.randomseed(os.time())` must not be inside a cycle. Just at the the beginning of your code. – Egor Skriptunoff Jun 19 '14 at 09:40
  • thanks @EgorSkriptunoff but i also tried to multiply i by os.time and it's work i was get the same value each time – Antwan Jun 19 '14 at 10:24
  • possible duplicate of [Lua math.random not working](http://stackoverflow.com/questions/18199844/lua-math-random-not-working) – Yu Hao Jun 19 '14 at 10:39
  • @Tony As Egor told you, you can't seed the RNG in your loop. "seeding" an RNG is an *initialization* step. If you keep initializing it, it will always return the same value. [See here](http://en.wikipedia.org/wiki/Random_seed). – Mud Jun 19 '14 at 13:27
  • @Mud but i get same result every time i run my code if i initialize my seed just one time time – Antwan Jun 19 '14 at 18:09
  • See the answer that Yu Hao linked to. You probably need to call random a few times before your loop. – Mud Jun 19 '14 at 18:30

1 Answers1

1

I'm not sure what the for i=1,fp loop is doing with temp and fo, for example the seed should only be set once, and also, the return value on line after local fo is ignored, seems very messy. But based on your post, if you really just want to randomly select N cells from your 2D array and set those to either 4 or 5 (randomly), this should work:

-- maybe N = fp
local N = 5
math.randomseed(os.time())
local i = 1
repeat
    fx = math.random(1, 10)
    fy = math.random(1, 10)
    if grid[fx][fy] == 0 then
        grid[fx][fy] = math.random(4,5)
        i = i + 1
    end
until i > N

Note however that the closer N is to number of items in array (100 in your example), the longer it will take for the loop to complete. If this is a concern, then for large N values, you could do the opposite: initialize each cell to 4 or 5 randomly, and then randomly set size - N of them to 0.

math.randomseed(os.time())

local rows = 10
local columns = 10
local grid = {}

if N > rows*columns/2 then 
    for i=1,rows do
        grid[i] = {}
        for j=1,columns do
            grid[i][j] = math.random(4,5)
        end
    end

    local i = 1
    repeat
        fx = math.random(1, 10)
        fy = math.random(1, 10)
        if grid[fx][fy] ~= 0 then
            grid[fx][fy] = 0
            i = i + 1
        end
    until i > N

else
    for i=1,rows do
        grid[i] = {}
        for j=1,columns do
            grid[i][j] = 0
        end
    end

    local i = 1
    repeat
        fx = math.random(1, 10)
        fy = math.random(1, 10)
        if grid[fx][fy] == 0 then
            grid[fx][fy] = math.random(4,5)
            i = i + 1
        end
    until i > N
end
Oliver
  • 27,510
  • 9
  • 72
  • 103
  • thanks @Scholli for your answer it's work but i don't know why some times i got same result each time i run that code same random value maybe the IDE cash some value or i don't know why i got like this result and for the second solution no it doesn't work in my case – Antwan Jun 23 '14 at 09:40
  • @Tony if you want the random numbers to be the same each time you run, you need to set the seed to some constant that you choose (do a web search for how to choose good seeds, but an arbitrary value like 7861231 should be fine). – Oliver Jun 23 '14 at 14:59