D
D
doctop2021-10-31 23:10:31
Lua
doctop, 2021-10-31 23:10:31

Why are there no multiple return values ​​when a function is called multiple times?

There is a working function that generates a word.

function password()
  local vars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
  local strlengths = string.len(vars)
  print(strlengths)
  local vars1 = ''
  math.randomseed(os.time())
  arrys = {}
  for vars2 in vars:gmatch"." do
    table.insert(arrys, vars2)
  end
  for i = 2, strlengths do
    vars1 = vars1 .. arrys[math.random(2, #arrys)] 
  end	
return vars1
end

I want to make a bunch of different words.
I call this function several times:
list={}
for i=1,10 do
  list[i]=password()
end

And the output is a pack with ten identical words :(
Tell me how to solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2021-11-01
@doctop

function password(num) ----> фикс
  num = (num or 0) * 5 ----> новая логика разных слов
  local vars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
  local strlengths = string.len(vars)
  print(strlengths)
  local vars1 = ''
  math.randomseed(os.time() + num) ----> фикс
  arrys = {}
  for vars2 in vars:gmatch"." do
    table.insert(arrys, vars2)
  end
  for i = 2, strlengths do
    vars1 = vars1 .. arrys[math.random(2, #arrys)] 
  end	
return vars1
end

list={}
for i=1,10 do
  list[i]=password(i) ----> фикс
end

PS I did not delve into the function itself. If the author of the question is sure that it works well, then with my edits it will also work well, at least for the purposes of obtaining different words. :)

D
doctop, 2021-11-01
@doctop

Indeed, the problem was in the seed. Thanks a lot!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question