Jump to content

math problem


Recommended Posts

So guys, Im stuck at this.

I want to divide a random number by 5.

value = math.random(100, 500)

how to do so? each divider also has random number, and when we sum them up result is the value.

for example value is 500, we divide it by 5 get random number 100, 210, 140, 50. I want to operate this but I dont know how to do to get these 5 random values.

Thanks.

Link to comment
  • Moderators

good question.

But try something like this:

-- setup -- 
local value = 500 
local splitUpInParts = 5 
----------- 
  
  
local valuePeaks = (100/splitUpInParts)/100  
local results = {} 
local valueInParts = math.floor((value/splitUpInParts)+0.5) 
local valueTotal = 0 
for i=1,splitUpInParts-1 do 
    local peak 
    if math.random(2) == 1 then 
        peak = math.random(math.floor((valueInParts*valuePeaks)+0.5)) 
    else 
        peak = -math.random(math.floor((valueInParts*valuePeaks)+0.5)) 
    end 
     
    local result = valueInParts+ peak 
    results[#results+1] = result 
    valueTotal = valueTotal+result 
end 
  
results[#results+1] =  value - valueTotal -- rest value 
  
  
for i=1,5 do 
    outputChatBox(results[i]) 
end 

This are the results from the demo

92

84

96

116

112

The results do not have extreme ups and downs, but at least they will not be lower then zero.

Link to comment
  
results = {} 
function divideThisValue(value) 
    local value = value 
    local splitUpInParts = 5 
    ------ 
  
    local valuePeaks = (100/splitUpInParts)/100 
    local results = {} 
    local valueInParts = math.floor((value/splitUpInParts)+0.5) 
    local valueTotal = 0 
    for i=1,splitUpInParts-1 do 
        local peak 
        if math.random(2) == 1 then 
            peak = math.random(math.floor((valueInParts*valuePeaks)+0.5)) 
        else 
            peak = -math.random(math.floor((valueInParts*valuePeaks)+0.5)) 
        end 
         
        local result = valueInParts+ peak 
        results[#results+1] = result 
        valueTotal = valueTotal+result 
    end 
    results[#results+1] = value - valueTotal 
end 
  
addEvent("onEstimatingOver", true) 
addEventHandler("onEstimatingOver", root, function() 
    onFactoryWindowClose() 
    if estimating then 
        estimating = false 
    end 
    valueof = getElementData(getLocalPlayer(), "Rocks:iron") or 0 
    if guiGetVisible(GUIEditors.window[1]) then 
        divideThisValue(valueof) 
        copper = results[1] 
        bronze = results[2] 
        silver = results[3] 
        iron = results[4] 
        gold = results[5] 
        guiSetText(GUIEditors.copper,  tostring(copper).." grams\n"..tonumber(copper*25).."$") 
        guiSetText(GUIEditors.bronze,  tostring(broze).." grams\n"..tonumber(bronze*35).." $") 
        guiSetText(GUIEditors.silver, tostring(silver).." grams\n"..tonumber(silver*45).." $") 
        guiSetText(GUIEditors.iron, tostring(iron).." grams\n"..tonumber(iron*46).." $") 
        guiSetText(GUIEditors.gold,  tostring(gold).." grams\n"..tonumber(gold*60).." $") 
    end 
end) 

I failed, what's wrong? these vars get nil value.

Link to comment
  • Moderators

Because results is defined 2 times and the one inside the function is a local.

function divideThisValue(value) 
    local value = value 
    local splitUpInParts = 5 
    ------ 
     
    local valuePeaks = (100/splitUpInParts)/100 
    local results = {} 
    local valueInParts = math.floor((value/splitUpInParts)+0.5) 
    local valueTotal = 0 
    for i=1,splitUpInParts-1 do 
        local peak 
        if math.random(2) == 1 then 
            peak = math.random(math.floor((valueInParts*valuePeaks)+0.5)) 
        else 
            peak = -math.random(math.floor((valueInParts*valuePeaks)+0.5)) 
        end 
        
        local result = valueInParts+ peak 
        results[#results+1] = result 
        valueTotal = valueTotal+result 
    end 
    results[#results+1] = value - valueTotal 
    return results 
end 
  
addEvent("onEstimatingOver", true) 
addEventHandler("onEstimatingOver", root, function() 
    onFactoryWindowClose() 
    if estimating then 
        estimating = false 
    end 
    local valueof = tonumber(getElementData(getLocalPlayer(), "Rocks:iron")) or 0 
    if guiGetVisible(GUIEditors.window[1]) then 
        local results = divideThisValue(valueof) 
        if results then 
            local copper = results[1] or 0 -- if it fails some how, which probably will not happen. 
            local bronze = results[2] or 0 
            local silver = results[3] or 0 
            local iron = results[4] or 0 
            local gold = results[5] or 0 
            guiSetText(GUIEditors.copper,  tostring(copper).." grams\n"..tonumber(copper*25).."$") 
            guiSetText(GUIEditors.bronze,  tostring(broze).." grams\n"..tonumber(bronze*35).." $") 
            guiSetText(GUIEditors.silver, tostring(silver).." grams\n"..tonumber(silver*45).." $") 
            guiSetText(GUIEditors.iron, tostring(iron).." grams\n"..tonumber(iron*46).." $") 
            guiSetText(GUIEditors.gold,  tostring(gold).." grams\n"..tonumber(gold*60).." $") 
        end 
    end 
end) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...