• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Random numbers

Blade33711

Member
Joined
Aug 6, 2012
Messages
133
Solutions
1
Reaction score
5
Location
Poland
I would like a loop in which random numbers from 1 to 10 are added so their sum is 100.
I have no idea how to take it. Can anyone help?
 
Solution
Maybe something like this:

Code:
function randTest127653()
  local sumTest = 0
  while sumTest < 100 do
    local rand = math.random(1, 10)
    if sumTest + rand > 100 then
      rand = 100 - sumTest
    end
    sumTest = sumTest + rand
    print('sumTest: '..sumTest..'  rand: '..rand)
  end
end

First: while -> if number is lower than 100
Second: rand numer from range 1-10
Third: Check if rand numer + sum is lower than 100 -> If don't -> calculate matching numer
Fourth: Add "rand" numer with "sum" variable
Fifth: Print loop result step (you must change this, create table and add value "rand" to this table in this line. After line 10 add "return table_name")
Maybe something like this:

Code:
function randTest127653()
  local sumTest = 0
  while sumTest < 100 do
    local rand = math.random(1, 10)
    if sumTest + rand > 100 then
      rand = 100 - sumTest
    end
    sumTest = sumTest + rand
    print('sumTest: '..sumTest..'  rand: '..rand)
  end
end

First: while -> if number is lower than 100
Second: rand numer from range 1-10
Third: Check if rand numer + sum is lower than 100 -> If don't -> calculate matching numer
Fourth: Add "rand" numer with "sum" variable
Fifth: Print loop result step (you must change this, create table and add value "rand" to this table in this line. After line 10 add "return table_name")
 
Last edited:
Solution
Back
Top