How to get a random number with 'weight'?

BlitzMax Forums/BlitzMax Programming/How to get a random number with 'weight'?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

here si a graphical example:

Graphics 1024,768,16

Type Ball
	Field image:timage
	Field r:Int , g:Int , b:Int
	Field weight:Int
	Field Drawn:Int
	
	Method draw()
		SetColor r , g , b
		DrawOval (Rand(0 , GraphicsWidth() ) , Rand(0 , GraphicsHeight() ) , 25 , 25)
		drawn:+1
	End Method	
End Type

Local redball:Ball = New Ball
redball.weight = 4
redball.r = 255

Local blueball:Ball = New Ball
blueball.weight = 16
blueball.b = 255


Local yellowball:Ball = New Ball
yellowball.weight = 33
yellowball.r = 255
yellowBall.g = 255

SetBlend ALPHABLEND
SetAlpha 0.25
SeedRnd(MilliSecs())

	While balls<100000
	
		If WeightedRND(redball.weight) = True Then redball.draw
		If WeightedRND(blueball.weight) = True Then blueball.draw
		If WeightedRND(yellowball.weight) = True Then yellowball.draw
		
		balls:+1
	
	Wend
Local scale:Float = 3
SetScale 3 , 3
SetAlpha 1
SetColor 255,255,200
DrawText "Red:" + redball.Drawn , GraphicsWidth()/2 ,  GraphicsHeight()/2
DrawText "Blue:" + blueball.Drawn , GraphicsWidth()/2 , scale*(TextHeight("X"))+GraphicsHeight()/2
DrawText "Yellow:"+ yellowball.Drawn,GraphicsWidth()/2 , scale*(TextHeight("X")*2)+GraphicsHeight()/2

Flip

WaitKey
	
	
	
	
Function WeightedRND(weight:Int)
	If Rand(0,weight*weight) = 1 Then Return True
End Function