generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Finding the median of three numbers

Previous Thread :: Next Thread 
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
07 Jul 2014 01:30 PM
How would I do this?
Report Abuse
ohno1112 is not online. ohno1112
Joined: 23 Mar 2013
Total Posts: 833
07 Jul 2014 01:31 PM
a = Number1 + number2 + number3
median = a/3
Report Abuse
domorox17 is not online. domorox17
Joined: 06 Mar 2012
Total Posts: 1710
07 Jul 2014 01:34 PM
That is the mean, or average...
Report Abuse
killajake25 is not online. killajake25
Joined: 31 Jan 2011
Total Posts: 1713
07 Jul 2014 01:40 PM
That would be the Arithmetic Mean.

OP wants the median.

1,2,3,4,5

Median = 3

2, 7 , 11 , 17, 23 , 29, 32

Median = 17

It is the middle result in a group of raw data in ascending order.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
07 Jul 2014 01:51 PM
For three numbers, you could use this:


local a, b, c = 12, 16, -167
local median = math.min(math.max(a, b), math.max(b, c), math.max(a, c))
print( median )



If you want more, I suggest using a table and sorting it.
Report Abuse
Conmiro is not online. Conmiro
Joined: 13 Oct 2008
Total Posts: 3393
07 Jul 2014 02:08 PM
-- Get the median of a table.
function stats.median( t )
local temp={}

-- deep copy table so that when we sort it, the original is unchanged
-- also weed out any non numbers
for k,v in pairs(t) do
if type(v) == 'number' then
table.insert( temp, v )
end
end

table.sort( temp )

-- If we have an even number of table elements or odd.
if math.fmod(#temp,2) == 0 then
-- return mean value of middle two elements
return ( temp[#temp/2] + temp[(#temp/2)+1] ) / 2
else
-- return middle element
return temp[math.ceil(#temp/2)]
end
end
Report Abuse
Conmiro is not online. Conmiro
Joined: 13 Oct 2008
Total Posts: 3393
07 Jul 2014 02:10 PM
-- Get the median of a table.
function statsmedian( t )
local temp={}

-- deep copy table so that when we sort it, the original is unchanged
-- also weed out any non numbers
for k,v in pairs(t) do
if type(v) == 'number' then
table.insert( temp, v )
end
end

table.sort( temp )

-- If we have an even number of table elements or odd.
if math.fmod(#temp,2) == 0 then
-- return mean value of middle two elements
return ( temp[#temp/2] + temp[(#temp/2)+1] ) / 2
else
-- return middle element
return temp[math.ceil(#temp/2)]
end
end

print(statsmedian({4,5,2,3,4}))
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image