Re: Gaussian Distribution Using Dice

new topic     » goto parent     » topic index » view thread      » older message » newer message

This looks good, but, again, I don't understand the last math part...

   http://mathforum.org/library/drmath/view/52207.html

I'd just like a FAST function like this:

   aNumber = 13 -- what's the odds of the sum of 13
   sides = 6 -- appearing when 6-sided dice
   rolls = 4 -- are rolled 4 times and added together?

   xFromY = instances( aNumber, sides, rolls )

Right now, I'm using a recursive algorithm, as follows:

-- start of code
--  code generated by Win32Lib IDE v0.18.20

 
include Win32Lib.ew
without warning

--------------------------------------------------------------------------------
--  Window Window1
constant Window1 = createEx( Window, "Window1", 0, Default, Default, 357, 300,
0, 0 )
constant statBar = createEx( StatusBar, "StatusBar8", Window1, 0, 0, 0, 0, 0, 0
)
constant txt_Rolls = createEx( EditText, "3", Window1, 52, 12, 48, 20, 0, 0 )
constant txt_Faces = createEx( EditText, "6", Window1, 52, 36, 48, 20, 0, 0 )
constant LText4 = createEx( LText, "Faces", Window1, 12, 40, 40, 20, 0, 0 )
constant LText5 = createEx( LText, "Rolls", Window1, 12, 16, 36, 20, 0, 0 )
constant LText6 = createEx( LText, "Distribution", Window1, 12, 72, 68, 20, 0, 0
)
constant pix_Graph = createEx( Pixmap, "pix_Graph", Window1, 216, 8, 100, 28, 0,
0 )
sequence IDEFlags
IDEFlags = classDefaults( Window, { {1, {WS_SYSMENU}},{2, {0}} } )
constant CWindow14 = createEx( Window, "CWindow14", Window1, 8, 92, 340, 160,
or_all({WS_CHILD, WS_CLIPSIBLINGS, WS_BORDER}), 0 )
openWindow(CWindow14, Normal)
moveZOrder( CWindow14, HWND_TOP)
---------------------------------------------------------
--------------------------------------------------------------------------------
include xcontrols.ew
include get.e

object junk

sequence rect, origin
integer x, y, width, height, pixX, pixY

constant
	GMID = xControl( Geometry, "", Window1, 0, 0, 0, 0, 0, 0 ),
	GMIDc = xControl( Geometry, "", CWindow14, 0, 0, 0, 0, 0, 0 )

manage( GMID, CWindow14, {0.0, 0}, {0, 90}, {1.0, 00}, {1.0 , 0} )
manage( GMIDc, pix_Graph, {0.0, 0}, {0.0, 0}, {1.0, 0.0}, {1.0 , 0.0} )

	rect = getCtlSize(CWindow14)
	width = rect[1]
	height = rect[2]

	setCtlSize( pix_Graph, width, height )
	setPenColor( pix_Graph, Black )
	
function sum(sequence s)
integer result
	result = 0
	for t=1 to length(s) do
		result += s[t]
	end for
	return result
end function

function increment(sequence face, integer which, integer max)
	face[which] += 1
	if face[which] > max then
		if which > 1 then
			face[which] = 1
			face = increment(face,which-1,max)
		end if
	end if
	return face
end function

function getMaxIndex(sequence s)
atom result, max
	max = -999999
	result = 0
	for t=1 to length(s) do
		if s[t] > max then
			max = s[t]
			result = t
		end if
	end for
	return result
end function

function getMax(sequence s)
atom max
	max = -999999
	for t=1 to length(s) do
		if s[t] > max then
			max = s[t]
		end if
	end for
	return max
end function

procedure plotGraph()
integer min, max, range, result, sides, rolls, toOne, thickness
sequence faces, results, dice, plotBase, plotPoint
atom scale

	rect = getCtlSize(CWindow14)
	width = rect[1]
	height = rect[2]

	origin = { 1 , height-1 }
	
	-- get values
	sides = getNumber(txt_Faces)
	rolls = getNumber(txt_Rolls)
	if sides > 0 and rolls > 0 and rolls <= 3 then
	min = rolls
	max = min * sides
	range = max - min + 1
	toOne = rolls - 1
	thickness = floor(width / range)
	-- plot graph
	dice = repeat(1,rolls)
	results = repeat(0,range)
	
	while dice[1] != ( sides + 1 ) do
		-- for 3d6, starts with {1,1,1}
		result = sum(dice)
		results[result-toOne]+=1
		dice = increment(dice,rolls,sides)
	end while

	scale = height / getMax(results)
	
	--plot the results
	clearWindow(pix_Graph)
	clearWindow(CWindow14)
	for t=1 to length(results) do
		plotBase = origin + {(t-1) * thickness,0}
		plotPoint = origin + { plotBase[1]+thickness, floor(-results[t]*scale) }
		if t/2 = floor(t/2) then
			setPenColor(pix_Graph,Blue)
		else
			setPenColor(pix_Graph,Black)
		end if
drawRectangle(pix_Graph,w32True,plotBase[1],plotBase[2],plotPoint[1],plotPoint[2])
		copyBlt(CWindow14, 1, 1, pix_Graph )
	end for
	setText(statBar, sprintf("%d,%d",origin))
	end if
	
end procedure

--------------------------------------------------------------------------------
procedure Window1_onPaint (integer self, integer event, sequence params)--params
is ( int x1, int y1, int x2, int y2 )
	plotGraph()
end procedure
setHandler( Window1, w32HPaint, routine_id("Window1_onPaint"))
--------------------------------------------------------------------------------
procedure Window1_onActivate (integer self, integer event, sequence
params)--params is ()
	plotGraph()
end procedure
setHandler( Window1, w32HActivate, routine_id("Window1_onActivate"))
--------------------------------------------------------------------------------
procedure txt_Rolls_onChange (integer self, integer event, sequence
params)--params is ()
	plotGraph()
end procedure
setHandler( txt_Rolls, w32HChange, routine_id("txt_Rolls_onChange"))
--------------------------------------------------------------------------------
procedure txt_Faces_onChange (integer self, integer event, sequence
params)--params is ()
	plotGraph()
end procedure
setHandler( txt_Faces, w32HChange, routine_id("txt_Faces_onChange"))
--------------------------------------------------------------------------------
procedure CWindow14_onResize (integer self, integer event, sequence
params)--params is ( int style, int cx, int cy )
	plotGraph()
end procedure
setHandler( CWindow14, w32HResize, routine_id("CWindow14_onResize"))


WinMain( Window1,Normal )
--end of code


-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu