Stochastic methods

From KokkugiaWiki

random numbers
random numbers are generated in rhinoscript using the vb command:
Rnd
We can multiply this by any value we like or constain it to an integer.
this example demonstrates how to use random numbers to generate random point locations.

Option Explicit

'Call rndPoints()

Sub rndPoints()
	
	Dim nPts, i
	
	nPts = Rhino.GetReal("how many points do you want", 50)
	
	' loop and make random points
	
	For i = 0 To nPts

		' add a point
		Rhino.AddPoint(Array(rnd*10,rnd*10,rnd*2))
				
	Next
		
End Sub

random choice
It is sometimes useful for random decision making when constrained to an integer.


Dim intChoice

intChoice = CInt(rnd*2)

Select Case intChoice
Case 0
   'do something
Case 1
   'do something else
Case 2
   'do something different to case 0 or case 1 actions
End Select

random growth
below is an example of random growth. this script is quite random indeed! and not so useful to you. You can try augmenting it by adding a target position and using negative feedback to head towards a target position - (see distance culling in the feedback tutorial:rhinoScript feedback methods)

Option Explicit
'random growth algorithm
'author: Robert Stuart-Smith, e: rob@kokkugia.com w: www.kokkugia.com

Call Main()
Sub Main()
	Dim i,strObj, strObjNew, arrPt
	Dim arrPtObj

	Randomize   ' Initialize random-number generator.

	strObj = Rhino.GetObject
	
	ReDim arrPtObj(0)
	
	For i = 0 To 1000
		ReDim Preserve arrPtObj(i)
		arrPt = Rhino.PointCoordinates (strObj)
		arrPtObj(i) = arrPt
		strObjNew = Rhino.CopyObject (strObj, arrPt, Array(arrPt(0) + (10* Rnd - 10* Rnd), arrPt(1) + (10* Rnd - 10* Rnd),arrPt(2) + (10* Rnd - 10* Rnd)))
		strObj = strObjNew		
	Next
	
	Rhino.AddCurve arrPtObj, 1
	
End Sub
Views