How to find the lowest value of a moon matrix?

Asked

Viewed 458 times

4

I made this code that makes an Array (at least I think it’s doing) and then writes all the values in it. Obs: The values are random. I need to find the lowest matrix value, but the logic I’ve always used gives me a predictable result.

Example: Let’s say the range of the invertalo is given by x and y. Being x the lowest value that can be assumed and y (In code x=0 and y=2500). In code, the value of the smallest variable is always giving x + 2, ie in my code the variable minor is always resulting in the value 2. If I set the lowest range value to 5, for example, at the end the value of variable smaller will be 7.(x=5, x+2 = 7)

matriz={}
for l=1,20 do
    matriz[l]={}
    for c=1,40 do
        matriz[l][c] = math.random(0,2500)
    end
end
menor = matriz[1][1]--Inicia na menor posição possivel
for i=1,20 do
    for j=1,40 do
        print("O valor na Linha:"..i.." Coluna:"..j.." é "..matriz[i][j])
        if matriz[i][j]<menor then-- essa parte deveria encontrar o meno valor
            menor = matriz[i][j]
        end--Final
    end
end
print("O menor valor encontrado é:"..menor)

This is the code I made, how I’m starting, I’m not understanding where his mistake is.

  • when I hit my eye on the code I noticed this line menor = matriz[1][1], start your variable menor with an infinite number or greater than the range of random numbers entered

  • I started the smallest variable with the value above the range of random numbers and the problem persisted.

  • Se o intervalo dos números sorteados é 0 e 5, no final a variável Menor irá conter o valor 0 + 2, ou seja 2. ??? I don’t understand ....

  • I gave an improved example. Short and thick: The value of the smallest variable is always the minimum value of the range plus 2.

  • this is a problem of the moon on some platforms, sometimes it always generates the same sequence, before the math.random insert math.randomseed(os.time())

  • Now that I’ve noticed that he’s actually generating the same values over and over again. In case the problem is on the platform, correct?(My concern is to know if the logic is wrong) Obs: I’m using Scite to compile. I didn’t understand the part of entering the randomseed before, it could be a little more specific?

  • the logic is correct, do not forget to always start your minor variable as indicated in my first comment ...

Show 2 more comments

1 answer

1

As suggested by @ederwander, there was a command to prepare the generation of pseudo-random numbers. Otherwise your logic is correct.

#!/bin/lua

math.randomseed(os.time()) -- faltava isso

matriz = {}

for l=1,20 do
    matriz[l] = {}
    for c = 1,40 do
        matriz[l][c] = math.random(0,2500)
    end
end

menor = matriz[1][1] -- Inicia na menor posição possivel

for i = 1,20 do
    for j = 1,40 do
        print("O valor na Linha:"..i.." Coluna:"..j.." é "..matriz[i][j])
        if matriz[i][j] < menor then -- essa parte deveria encontrar o meno valor
           menor = matriz[i][j]
        end -- Final
    end
end

print("O menor valor encontrado é:" .. menor)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.